Python modules are a convenient way to encapsulate and organize reusable code. If you find yourself copying and pasting the same code across multiple scripts or projects, it’s a good indication that you should consider creating a custom module. In this article, we’ll guide you through the process of creating and using your custom Python module.
1. Understand the Basics
A module in Python is simply a file containing Python definitions and statements. The file name is the module name followed by the .py extension. For instance, a module named mymodule would be saved as mymodule.py.
2. Create the Module
For our example, let’s assume you have a few mathematical functions that you want to reuse across projects. Then you can write them in a single file and then reuse them as module in other Python scripts.
For the example, I have created below mymath.py python script that includes, add, subtract, multiply, and divide functions.
# mymath.py
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
if y == 0:
return "Undefined (division by zero)"
return x / y
Here, we’ve created a simple module mymath.py with four basic arithmetic functions.
3. Use the Module
Now, let’s see how we can use this custom module in another Python script. Here we can use the import statement with the Python module script filename without extension. See below example:
# main.py
# Importing our custom module
import mymath
# Using functions from our module
print(mymath.add(5, 3))
print(mymath.subtract(5, 3))
print(mymath.multiply(5, 3))
print(mymath.divide(5, 3))
Note: Ensure that both main.py and mymath.py are in the same directory, or that mymath.py is in a directory that’s in Python’s sys.path.
4. Using the from…import Statement
In some cases, you may need to use a few functions only from the module. Then, instead of importing the whole module, you can also import specific functions:
from mymath import add, subtract
print(add(5, 3))
print(subtract(5, 3))
5. Using __name__
In larger modules, you might want to test your code. The __name__
attribute allows you to check if the module is being run directly or imported elsewhere:
# mymath.py
def add(x, y):
# ... (same as above)
# ... other functions ...
if __name__ == "__main__":
print("Running tests for mymath module")
assert add(2, 3) == 5
# ... more tests ...
6. Organize Multiple Modules with Packages
If you have multiple related modules, you can organize them into a package. A package is simply a way of collecting related modules together within a single directory hierarchy.
For example, to create a package named mypackage:
- Create a directory named mypackage.
mkdir mypackage
- Add an empty file named __init__.py to the directory (this makes Python treat the directory as a package or subpackage).
touch ./mypackage/__init__.py
- Place related module files inside the mypackage directory.
- Then, you can import modules from this package using:
from mypackage import mymath
7. Distributing Your Module
If you think your module would benefit the wider Python community, consider distributing it via the Python Package Index (PyPI). First, you’d package it using setuptools, then you can distribute it with twine.
Conclusion
Creating and using custom Python modules is an essential skill, especially as your projects grow in complexity. Modules help in breaking down complex systems, promoting code reuse, and maintaining a cleaner codebase. As you continue your Python journey, think of modules as your trusty toolkit, ever ready to help you build great things.