Python Package
In Python, a package is a way of organizing related modules into a directory hierarchy. Essentially, it's a directory that contains multiple module files and a special __init__.py
file to let Python know that the directory is a package.
Here's a step-by-step guide on how to organize your modules into a package:
Create a directory - This directory will be your package. Let's call it
mypackage
.
mypackage/
Add a
__init__.py
file - This file is required for Python to recognize the directory as a package. It can be empty, but it can also execute initialization code for the package.
mypackage/
__init__.py
Add module files - Now you can add your Python module files to the package.
mypackage/
__init__.py
module1.py
module2.py
In this example, module1.py
and module2.py
are Python files with their own functions, classes, and variables.
Use the package - You can now import the modules in the package from another Python script using the package name and the module name.
# main.py
from mypackage import module1, module2
module1.my_function()
module2.my_function()
In this example, my_function
is a function defined in module1.py
and module2.py
.
Subpackages - Packages can also contain subpackages. A subpackage is just a package that is a subdirectory of another package.
mypackage/
__init__.py
module1.py
module2.py
subpackage/
__init__.py
module3.py
You can import modules from subpackages like this:
from mypackage.subpackage import module3
module3.my_function()
Remember, organizing your code into packages and modules not only helps keep your codebase clean and manageable, but also makes it easier to reuse your code across different projects. Happy coding! 🚀
Last updated