How to Import and Use Functions from Another Python File
Splitting your Python code into multiple files is a core skill for organizing larger projects and promoting code reuse. Python’s import
statement lets you access functions and variables defined in another file (called a "module") to keep your code clean, maintainable, and modular. This guide covers how imports work, common patterns, and practical examples to help you use them like a pro.
Table of Content
Why Import Functions from Another File?
- Reusability: Write functions once and use them in many programs.
- Organization: Keep related functions together by topic.
- Maintainability: Fix bugs or update code in just one place.
- Collaboration: Work as a team with well-separated code files.
Syntax & Structure
# Import an entire module (file)
import module_name
# Import specific function(s) from a module
from module_name import function_name
# Import with alias
import module_name as mn
from module_name import function_name as fn
# Import multiple functions
from module_name import func1, func2
module_name
: The name of the file without.py
(e.g.,my_utils.py
→my_utils
).function_name
: The function you want to use from the module.as
: Gives an imported name an alias for convenience.
Basic Import Examples
For all these examples, assume you have a file
math_utils.py
:
def add(a, b):
return a + b
def square(x):
return x * x
1. Import the Whole Module
import math_utils
result = math_utils.add(2, 3)
print(result)
Output
5
2. Import a Specific Function
from math_utils import square
print(square(4))
Output
16
3. Import Multiple Functions
from math_utils import add, square
result = add(5, square(2))
print(result)
Output
9
Import Variations & Aliases
1. Module Aliases (for shorter names)
import math_utils as mu
print(mu.add(6, 7))
2. Function Aliases
from math_utils import square as sq
print(sq(8))
3. Import All (not recommended)
from math_utils import *
print(add(1, 2))
print(square(3))
Note:
from ... import *
pollutes your namespace and can overwrite existing names. Use with care.Comparison Table: Import Styles
Pattern | Import Example | How to Use | Best For |
---|---|---|---|
Whole module |
|
|
Reusing many functions, clear code |
Specific function |
|
|
Simpler syntax, importing just what you need |
With alias |
|
|
Shorter names, avoiding conflicts |
Import all functions |
|
|
Quick scripts; avoid in large projects |
Useful Tips
- File must be in the same directory or Python path: Otherwise, you’ll get a
ModuleNotFoundError
. - No
.py
in module name: Just use the filename (e.g.,my_utils
notmy_utils.py
). - Prefer explicit imports: Use
from ... import
orimport ... as ...
for clarity and maintainability. - Reload after changes: If you edit a module and are using an interactive session, you may need to restart Python or use
importlib.reload()
. - Avoid circular imports: Structure your modules so that files do not import each other in a loop.
- Packages and directories: Use
import package.module
when organizing code in folders (add an__init__.py
file to the folder).
Conclusion
Using import
in Python is the foundation of writing organized, reusable, and maintainable code. Break your code into modules, import what you need, and use clear names for robust scripts and scalable projects.
Comments
Post a Comment