Python Package

Effective code organization becomes essential in Python as code sizes increase and projects get bigger. In bigger projects, this organization is accomplished through the usage of two constructs: modules and packages. Modules work as a library of code, allowing you to combine similar functions, classes, and code into a single file. By enabling you to bundle relevant modules together, packages go beyond this organizational framework. In a hierarchical system based on directories, developers can group modules into packages. Packages offer a sensible method for physically grouping and identifying related Python code segments.
Packages assist developers organize relevant modules, resolve conflicting module names, facilitate code distribution, and give a flat namespace a hierarchical organization. They act as an organizing tool, streamlining the module search path and adding information to imports. Packages make code easier to use, edit, and comprehend by grouping it together. This is especially useful as applications expand beyond a single file. One of the main justifications for creating packages is the ability to reuse code. Compared to classes, packages are larger, more conceptual pieces of code organization.
Creating New Packages
Usually, you establish a folder that will act as the package directory before creating a Python package. There will be one or more Python files (modules) in this directory. Importantly, when utilizing package syntax imports, a directory must contain a specific file called init.py to tell Python that it should be handled as a package.
The surrounding directory is identified as a package or sub package by the init.py file. The directory is not regarded as a package without this file. When the relevant package or sub package is imported, this file is immediately run. In the most basic scenario, it may be an empty file, but it may also include package initialization code. To correspond to that directory, variables assigned within the init.py file become attributes of the module object produced in memory. This can be helpful for gathering contents from nested modules or initializing data structures.
Sub packages, which are packages nestled inside the main package directory and have their own directories with init.py files and modules, can also be found inside a package. Particularly in very big packages, breaking up code into smaller packages keeps the package structure neat and orderly. However, when importing, highly nested sub packages may result in lengthy, dotted module names.
Code Example
Let’s demonstrate how to create a basic package structure. Take the example of a project directory called my project. We make a package called my package and a sub package called sub package inside of this.
Here is the directory structure:
my project/
├── main.py
└── my package/
├── init .py
├── module a.py
└── sub package/
├── init.py
└── module b.py
Now, let’s look at the contents of these files:
my package/ init.py:
# my package/ init.py
print("Initializing my package")
x = 1
my package/module a.py:
# my package/module a.py
def greet(name):
A simple greeting function.
print(f" Hello from module a, {name}!")
my package/sub package/init py:
# my package/sub package/ init.py
print("Initializing sub package")
my package/sub package/module b.py:
# my package/sub package/module b.py
def farewell(name):
A simple farewell function.
print(f" Goodbye from module b, {name}!")
main.py An example script using the package:
# main.py
# Import module a from my package
import my package .module a
# Import module b directly from the sub package
from my package sub package import module b
# You can also import specific functions/classes
from my package module a import greet
print("Starting main.py")
# Accessing components using dot notation
my package module a greet("Alice")
greet("Bob")
module b farewell("Charlie")
# Accessing the variable from init.py
print(f" my package x = {my package x}")
Usually, output showing the packages’ initialisation and the outcomes of running the functions appears when main.py is executed. The init.py files’ print statements show what happens when they are run. The example demonstrates how to access the functions and variables defined within packages and subpackages, as well as how to import modules from them using dot notation.