Organizing code into modules and packages
As your Python program grows, it’s helpful to organize code into multiple files and directories. Python supports this through modules and packages. This modular organization makes code more maintainable and reusable.
Modules: A module is simply a file containing Python code (definitions and statements). The module’s name is the file name without the .py extension. For example, if you have a file helpers.py with some function definitions, that file is a module named helpers. Modules allow you to logically separate code (for instance, grouping related functions together) and reuse code across multiple programs by importing the module.
Why use modules? If you write a longer program, you may want to split it into multiple files for easier maintenance, or reuse a function in several programs without copy-pasting it into each. Python lets you import definitions from one module into another. When you import a module, you can then access its functions, classes, and variables using the module’s name as a prefix (or import specific names directly). For example, if helpers.py defines a function compute(), you can use it in another script by writing import helpers and then calling helpers.compute().
Creating a Module: To create a module, all you need to do is write a Python .py file. For instance, let’s create a simple module file named mymath.py:
# contents of mymath.py
PI = 3.14159
def circle_area(radius):
return PI * radius * radius
def square_area(side):
return side * side
We save this file in the same directory as our main program. Now we can use this module in another script:
import mymath
print("Pi is:", mymath.PI)
print("Circle area:", mymath.circle_area(5))
print("Square area:", mymath.square_area(5))
When we run the main program, Python will load mymath.py (execute it) and make its functions and variables available through the mymath namespace. The output might be:
Pi is: 3.14159
Circle area: 78.53975
Square area: 25
You can also use from mymath import circle_area to import a specific function directly, or even from mymath import * to import everything (though the latter is usually discouraged as it can clutter your namespace).
Note: The first time a module is imported in a session, Python executes the code in that module (initializing variables, defining functions, etc.). Subsequent imports of the same module reuse the already-loaded module (unless you explicitly reload it).
Packages: A package is a way of organizing related modules into a directory hierarchy. A package is essentially a folder that contains multiple module files. What signals to Python that a folder is a package is the presence of an __init__.py file in that folder. (In modern Python, an __init__.py can be empty; its mere presence makes the directory a package. Without it, Python won’t recognize the directory as a package for import purposes.)
Think of a package as a collection of modules under a common namespace. For example, you might have a package shapes which contains modules circle.py, square.py, triangle.py, etc. To create this package:
-
Create a directory named
shapes. -
Inside
shapes, create an empty file__init__.py(this can be empty or can execute package initialization code). -
Create your module files
circle.py,square.py, etc., inside theshapesdirectory.
The directory structure might look like:
shapes/ <-- package directory
__init__.py <-- special file to mark a package
circle.py <-- module for circle-related functions
square.py <-- module for square-related functions
triangle.py <-- module for triangle-related functions
Now, if this shapes directory is in your Python module search path (for instance, in the same directory as your main script), you can import from the package. For example:
import shapes.circle
from shapes import square
area = shapes.circle.area(5) # call function area() from circle module
side = 4
print("Square area:", square.area(side))
Here, shapes.circle refers to the circle.py module within the shapes package. We could also do from shapes.circle import area to import a specific function from that module. The package provides a hierarchical namespace (shapes -> circle) to organize modules.
The __init__.py in a package can also execute code. Often it’s left empty, but you can use it to set up package-level variables, or to import certain submodules for easier access. For instance, you might have __init__.py do from .circle import area as circle_area so that a user can do from shapes import circle_area directly. The . in import means relative import within the package (e.g., from . import circle within shapes/__init__.py would import the circle module from the same package).
Using Modules and Packages: When you use imports, keep in mind:
-
Use
import module_nameto import an entire module. Then you refer to its contents withmodule_name.xsyntax. -
Use
from module_name import name1, name2to import specific things directly. -
Use
import package.moduleto import a submodule from a package. -
You can use
asto give an alias to a module: e.g.,import numpy as np(common in data science) or in our example,import shapes.circle as circle_mod. -
A module is loaded only once per interpreter session. If you edit the module file and want to see changes without restarting Python, you need to reload it (using
importlib.reload, for advanced use).
Example Package Usage: Suppose our shapes package’s circle.py defines area(radius) and circumference(radius), and square.py defines area(side) and so on. We could use them as:
from shapes.circle import area as circle_area
from shapes.square import area as square_area
print(circle_area(3)) # uses shapes/circle.py's area
print(square_area(3)) # uses shapes/square.py's area
This is straightforward and prevents name collisions by specifying which module’s function we want (we aliased them to avoid both being called just area in our code).
When to create a package? If you have just a few modules, a package might not be necessary. But if you have many modules that logically belong together (like a toolkit of related utilities), grouping them in a package helps avoid name conflicts with other modules and clarifies their relationship.
Summary: Organising code into modules and packages is key for larger programs:
-
A module is a single Python file that can be imported. It allows code reuse across scripts.
-
A package is a directory of modules (and possibly subpackages) with an
__init__.py, providing a nested namespace for organization.
Use modules and packages to structure code by functionality (e.g., one module for database logic, another for computations, etc.), making it easier to maintain and navigate. Python’s standard library itself is a collection of many modules and packages.
By splitting your code into modules, you also enable easier testing (you can test each module’s functionality in isolation) and easier collaboration (different team members can work on different modules without stepping on each other’s toes). Remember to document your modules and packages (via docstrings and README files) so users know how to use them.
