Python Creating Modules

In the last lesson, we learned about the Python Import Statement. Now let us create our own modules and reuse their code in other files.

How to Create a Module in Python

A module is just a file with a .py extension. We can put any Python code inside.

Let us create a file called calculator.py.

calculator.py
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
if b == 0:
return "Cannot divide by zero"
return a / b

That is a module. It has four functions.

How to Use Our Module in Another File

Now we create another file called main.py in the same folder.

main.py
import calculator
print(calculator.add(5, 3))
print(calculator.subtract(10, 4))
print(calculator.multiply(6, 7))
print(calculator.divide(20, 5))

Output

8
6
42
4.0

We imported the calculator module and used its functions.

Using the name Variable in Python

Every Python module has a special variable called __name__. When a module is run directly, __name__ is "__main__". When it is imported, __name__ is the module’s name.

We can use this to write code that only runs when the module is run directly, not when it is imported.

calculator.py
def add(a, b):
return a + b
def subtract(a, b):
return a - b
if __name__ == "__main__":
print("Testing calculator module")
print(add(5, 3))
print(subtract(10, 4))

If we run calculator.py directly, we see:

Testing calculator module
8
6

But if we import it from main.py, the test code does not run.

main.py
import calculator
print(calculator.add(5, 3))

Output

8

That is a very common pattern. It lets us test our module without affecting other programs that use it.

Adding Documentation to a Module

We can add a docstring at the top of our module to describe what it does.

calculator.py
"""
This module provides basic arithmetic functions.
It includes add, subtract, multiply, and divide.
"""
def add(a, b):
return a + b
# rest of the functions...

That helps others understand what the module is for.

A Real-Life Example – String Utilities Module

Let us create a module with common string helpers.

string_utils.py
def reverse_string(s):
return s[::-1]
def is_palindrome(s):
s = s.lower()
return s == s[::-1]
def count_vowels(s):
return sum(1 for c in s.lower() if c in "aeiou")
if __name__ == "__main__":
print(reverse_string("hello"))
print(is_palindrome("racecar"))
print(count_vowels("programming"))

Now we can use this module in our main program.

import string_utils
print(string_utils.reverse_string("Python"))
print(string_utils.is_palindrome("madam"))
print(string_utils.count_vowels("Hello World"))

Output

nohtyP
True
3

That is a useful module.

Common Mistakes When Creating Modules in Python

Mistake 1: Naming a module with spaces

# my module.py

This is not allowed. Module names should not have spaces.

Fix: Use underscores.

my_module.py

Mistake 2: Forgetting to add name == β€œmain”

If we put test code in a module, it runs every time the module is imported. That can cause problems.

Fix: Put test code inside if __name__ == "__main__":.

Mistake 3: Circular imports

If module A imports module B, and module B imports module A, we get a circular import. That can cause errors.

Fix: Reorganize the code to avoid circular dependencies.

Mistake 4: Using a module name that conflicts with a built-in

Do not name your module math.py or random.py. It will shadow the built-in modules.


Next, we will learn about Python Packages and organize related modules inside folders.