Introduction
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, that's a good indication that you should consider creating a custom module. In this article we'll walk you through the process of creating and using your own custom Python module.
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 example, a module named mymodule would be saved as mymodule.py.
Create the form
For our example, let's say you have some mathematical functions that you want to reuse in projects. So you can write them in a single file and then reuse them as a module in other Python scripts.
For example, I've created the Python script mymath.py below that includes addition, subtraction, multiplication, and division 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 have created a simple mymath.py module with four basic arithmetic functions.
Use the form
Now let's see how we can use this custom module in another Python script. Here we can use import statement with Python module script file name without extension. See the example below:
# 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: Make sure that both main.py and mymath.py are in the same directory, or that mymath.py is in a directory that is in Python's sys.path.
Using the from...import statement
In some cases, you may need to use some functions only from the module. So instead of importing the entire module, you can also import specific functions:
from mymath import add, subtract
print(add(5, 3))
print(subtract(5, 3))
Using __name__
In larger modules, you may want to test your code. The __name__ attribute allows you to control whether the module is 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...
Organize multiple modules with packages
If you have multiple related modules, you can organize them into one package. A package is simply a way to collect related modules together within a single directory hierarchy.
For example, to create a package named mypackage:
Create a directory called mypackage.
mkdir mypackage
Add an empty file called __init__.py to the directory (this makes Python treat the directory as a package or subpackage).
touch./mypackage/__init__.py
Place the related module files in the mypackage directory.
Then, you can import modules from this package using:
from mypackage import mymath
Deploy the form
If you think your module could benefit the broader Python community, consider distributing it via the Python Package Index (PyPI). First, you'll 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 become increasingly complex. Modules help break down complex systems, promote code reuse, and maintain a cleaner code base. As you continue your journey with Python, think of modules as your trusty toolkit, always ready to help you create great things.