🚀 Supercharge your YouTube channel's growth with AI.
Try YTGrowAI FreeCreating Python Modules

When one Python file needs to use a function defined in another, you can put that function in a module. A Python module is a source file that another file can import.
In this piece, I’ll show how to save reusable code in a .py file and import it from another script.
TL;DR
A Python module is a .py file that another file can import by its filename without the extension.
- Save reusable definitions in a file such as metrics.py.
- Put the importing script in the same directory for this basic example.
- Use import metrics, then call metrics.rectangle_area().
- Keep test or command-line code under if __name__ == "__main__":.
What Is a Python Module?
A module is a Python source file that can be imported. The filename metrics.py is imported as metrics.
| Python file | Import name |
|---|---|
| metrics.py | metrics |
| use_metrics.py | use_metrics |
The module provides a namespace for its definitions. For example, metrics.rectangle_area calls the function from metrics.py. Functions in that file use its global names when they run, even if another script imports one by name.
For more import forms, see AskPython’s guide to Python modules. The small example here stays with one file and one importing script.
How to Create and Import a Python Module
Create one file for reusable code and a second file that imports it. I ran the second file from the same directory as metrics.py; it printed 12, the area returned for a rectangle with sides 4 and 3.
Step 1: Save reusable code in a module file
Create metrics.py and define the function there.
# metrics.py
def rectangle_area(width, height):
return width * height
if __name__ == "__main__":
print(rectangle_area(4, 3))
Here, rectangle_area is available to the importing script. The __name__ value is "__main__" when Python runs this file as the program entry point.
Step 2: Import the module from another file
Use import metrics and then access a function with dot notation.
import metrics
print(metrics.rectangle_area(4, 3))
Run the importing file from the directory containing both files. The exact command is:
python3 use_metrics.py
The captured output is:
12

The output confirms that the function returned the expected area, 12. The imported definition runs from metrics.py.
Step 3: Import a function by name
Use from metrics import rectangle_area when the importing file needs that function directly. The function can then be called without the module prefix.
from metrics import rectangle_area
print(rectangle_area(4, 3))
This form is useful for a small, specific import. For several definitions from one module, import the module so calls keep their source visible.
In Jupyter, the notebook interface sends code to a separate kernel process. That kernel executes cells and retains interpreter state, including imports, so keep the module in a directory it can import. Restart the kernel or reload the module after editing its source, then consult the Jupyter architecture documentation for the kernel model.
Step 4: Run the module directly
I ran metrics.py directly, and its guarded print produced the same area as the import example.
python3 metrics.py
12

The __name__ value is "__main__" for the directly run file and “metrics” when use_metrics.py imports it.
Import Errors and Module Edge Cases
Import failures often come from a filename collision or a different launch context.
| Symptom | Likely cause | Check or fix |
|---|---|---|
| ModuleNotFoundError | Python cannot locate the module, or the imported name differs | Check the spelling and launch from the project setup that includes the module |
| An unexpected module loads | A local filename shadows a standard or installed module | Rename the local file so it has a distinct name |
| Output appears during import | Top-level statements ran as Python loaded the file | Move script-only work under the __main__ guard |
Python searches configured directories in sys.path, and the set depends on how the interpreter starts. The official module search path documentation describes the search process.
A local file can hide another module if both use the same name. For example, random.py can interfere with code that expects Python’s random module. For a normal source module, check its __file__ attribute to confirm which file loaded, then rename a colliding project file.
Within a package, sibling modules can use absolute or relative imports, though a top-level script needs absolute imports because it has no package context.
- A regular package is a directory that groups modules, commonly with an __init__.py file. Python also supports namespace packages that omit it. The file can run package initialization code when the package is imported.
- Keep imports near the top of a script so its dependencies are visible. Use an import inside a function only when conditional or delayed loading has a concrete purpose.
- Python may store compiled bytecode in .pyc files under __pycache__. These are generated from source and managed by Python, so keep the .py file as the part you edit.
When code must install into other projects, a local module alone is not enough. Package distributions need project metadata and build configuration. The Python Packaging User Guide covers that separate workflow.
Python’s built-in dir function lists names exposed by a module. It also shows attributes supplied by the interpreter, so identify your own functions and variables among them.
For a refresher on the functions you place in a module, see Python functions. Keeping the function focused makes it simpler to import and reuse.
Conclusion: Keep Modules Small and Importable
Keep reusable definitions in modules and choose clear names. The Python module tutorial explains the import model and packages, while AskPython’s module guide covers more import forms.
FAQ
These answers cover common questions about creating and importing local modules.
How do I create a Python module?
Save Python definitions in a file with a .py extension. Import the file from another script using its filename without the extension.
How do I import a function from my module?
Use from module_name import function_name, then call function_name directly. Use import module_name when you want to keep the module prefix.
Why can Python not find my module?
Check the filename and whether the module’s directory is in Python’s module search path for the way you launched the importing script.
Does a Python module need __init__.py?
A single .py module file does not. A regular package commonly includes __init__.py, while namespace packages can omit it.
Can I import a Python module in Jupyter?
Yes. The notebook kernel must be able to find the module directory, and it keeps interpreter state between cells.
Can a Python module also run as a script?
Yes. Put script-only behavior under if __name__ == “__main__” so it runs directly and stays inactive on import.


