Lesson 25: Modules: creating your own and importing it
A module is just a Python file whose functions you can reuse. Write mymath.py with two functions, import it from main.py, and the same code becomes available in every program in that folder.
In short
- A module is a `.py` file you import; its functions, variables and classes become available.
- `import mymath` keeps the namespace and you call `mymath.square(6)`.
- `from mymath import square` imports one name directly; `import mymath as mm` gives it a short alias.
- A file with `if __name__ == "__main__":` can be both imported and run on its own.
2
import styles in this lesson: import X and from X import y
Python docs
1
condition that separates "run" from "import": __name__ == "__main__"
Python docs
What a module is
A module is a Python file that other files can import. Nothing more: mymath.py is a module, and once it is in the same folder as your program, import mymath gives you everything defined inside it.
This is how code is reused without copying. Write a function once, import it in ten programs.
Step 1: the module file
# mymath.py — one file, two functions and a constant
def square(n):
return n * n
def cube(n):
return n * n * n
PI = 3.14159
Step 2: the program that uses it
# main.py — both files must be in the same folder
import mymath
from mymath import cube
import mymath as mm
print(mymath.square(6)) # 36
print(cube(3)) # 27
print(mm.PI) # 3.14159
How it works, step by step
import mymathrunsmymath.pyonce and stores its contents under the namemymath.- You then reach inside it with a dot:
mymath.square(6). Keeping the module name makes it obvious where the function came from. from mymath import cubecopies one name into your file, so you can callcube(3)directly. Convenient, and slightly harder to trace later.import mymath as mmis the same module under a shorter name, which is normal for long names.- Python looks for
mymath.pyfirst in the current folder, then in the standard library folders.
What it prints
36 27 3.14159
Making a file work both ways
def square(n):
return n * n
if __name__ == "__main__":
print("Run directly, square(4) =", square(4))
When you run the file, __name__ is "__main__" and the test passes. When another file imports it, __name__ is "mymath" and the print is skipped. This is how a module can carry a small self-test without printing noise into every program that imports it.
Import styles
| Statement | Effect | Use when |
|---|---|---|
import mymath | Everything, under the module name | Default choice |
from mymath import cube | One name, directly | One function used often |
import mymath as mm | Short alias | Long module names |
from mymath import * | Everything, no prefix | Almost never: names collide silently |
Common mistakes
- Naming your file after a standard module, such as
math.py. Your file wins, and the real library stops loading. - Running the program from another folder, so the module is not found:
ModuleNotFoundError. - Importing the same module in a loop. Python caches it, so it is not an error, just noise.
Practice
Try these before moving on. Each one uses only what this lesson covered.
- Program 15: create `mymath.py` with square and cube functions, then import it in another file and print both results.
- Add a `PI` constant to your module and print it from the importing file.
- Import one function from your module with `from … import …` and call it without the module name.
Sources
Frequently asked questions
What is a module in Python?
A file containing Python code — functions, variables, classes — that another file can import. Any .py file in the same folder can be a module.
How do I create and import my own module?
Save the code as mymath.py, then write import mymath in your program and call mymath.square(6). Both files must be in the same folder, or the module must be on sys.path.
What is the difference between import x and from x import y?
import x keeps everything under the module name, so you write x.y. from x import y brings y into your namespace, so you call it directly. The first is clearer in larger programs.
What does if __name__ == "__main__" do?
It runs the block only when the file is executed directly, not when it is imported. That lets a module carry a test or a demo without affecting the programs that use it.