CodeGym /Courses /Python SELF EN /Creating Your Own Package

Creating Your Own Package

Python SELF EN
Level 19 , Lesson 1
Available

2.1 Introduction to Packages

Packages are a way to organize modules in directories. A package is a directory containing an __init__.py file and one or more modules. The __init__.py file can be empty or contain initialization code for the package.

Example of a Package/Directory Structure


mypackage/
    __init__.py
    module1.py
    module2.py
Contents of __init__.py

# __init__.py
from .module1 import func1
from .module2 import func2
Contents of module1.py

# module1.py
def func1():
    return "Function 1"
Contents of module2.py

# module2.py
def func2():
    return "Function 2"

Example code that uses functions from this package:


import mypackage

print(mypackage.func1())  # Output: Function 1
print(mypackage.func2())  # Output: Function 2

Let's try to write our own package below and understand how others work.

2.2 Creating Your Package Step-by-Step

Step 1: Creating the Package Structure

The first step is to create the directory and file structure for your package. As an example, we'll create a package called mypackage containing two modules: module1.py and module2.py.

Example of package structure:


mypackage/
    __init__.py
    module1.py
    module2.py

File descriptions:

  • __init__.py: This file can be empty or contain initialization code for the package. It's needed so that Python treats the directory as a package.
  • module1.py and module2.py: These files contain functions, classes, and variables that you want to include in your package.

Step 2: Writing Code for the Modules

Let's write some simple code for our modules.

module1.py


def func1():
    return "Function 1 from module1"

module2.py


def func2():
    return "Function 2 from module2"

__init__.py

To import functions from our modules at the package level, let's add them to __init__.py.


from .module1 import func1
from .module2 import func2

Now, when you import the package, the functions func1 and func2 will be available directly.

Step 3: Testing the Package

Create a file test.py in the same directory where the mypackage directory is located, and use the package in this file for testing.

test.py


import mypackage

print(mypackage.func1())  # Output: Function 1 from module1
print(mypackage.func2())  # Output: Function 2 from module2

Run test.py to make sure your package is working correctly.

2.3 Using the dir() Function

When you import a module or package, you can use the dir() function to get a list of all available attributes and methods in that module. This is especially useful if you're working with a new or unfamiliar module and want to quickly find out what functions and classes it provides.

Example: Exploring the Standard math Module

Let's look at an example of using dir() to explore the standard math module.


import math

print(dir(math))

This code imports the math module and prints a list of all available attributes and methods. The result will look something like this:


['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan',
'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 
'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf',
'isnan', 'isqrt', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'perm', 'pi', 'pow', 'prod',
'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']

This list includes all the functions and constants defined in the math module, like sin, cos, tan, pi, and others.

Using dir() for Custom Modules

You can also use dir() for your own custom modules. Suppose you have a file mymodule.py with the following content:


# mymodule.py
def greet(name):
    return f"Hello, {name}!"
            
pi = 3.14159

You can import this module and use dir() to explore its contents:


import mymodule

print(dir(mymodule))

The result will be:


['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 
'greet', 'pi']

The list includes eight special attributes (surrounded by underscores) of the mymodule, and at the end, your method greet and variable pi.

2
Task
Python SELF EN, level 19, lesson 1
Locked
Exploring a Module
Exploring a Module
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION