How do I call a function from another .py file?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In Python, programs often grow beyond a single file, requiring the use of functions, classes, and variables from other files. Importing and calling functions from another .py file is both a fundamental and a critical aspect of effective Python programming. This allows for code modularity, reusability, and better organization.
Basics of Importing in Python
Before calling a function from another file, you must understand how to import it. Python uses the import statement to fetch definitions from another file into your current script.
Assume you have two Python scripts:
utility.py— contains function definitions.main.py— needs to access functions fromutility.py.
Example of utility.py:
Example of importing and using in main.py:
In the above example, utility.py is imported to main.py, and its function add() is accessed using the syntax utility.add().
Different Ways to Import
- Import the whole module: This has been shown above where you import the file and use the module name to call its functions.
- Import specific functions: You can also import specific functions from a module:
- Import with aliases: If necessary, you can import modules or functions with aliases to avoid naming conflicts or for simpler syntax:
- Import all functions: You can import all functions from a module, though this is generally discouraged due to potential namespace conflicts:
Managing Imports and Circular Dependencies
Import statements can be placed at the beginning of a Python file, which is the conventional method. However, if handling circular dependencies, you might need to import modules or functions inside functions or conditionally.
Organizing Code with Packages
As projects grow, functions might span across different folders. Python treats folders as packages if they contain an __init__.py file. This file can be empty but it indicates that Python should treat the directory as a package.
Directory Structure Example:
Usage in main.py:
Summary Table
| Approach | Syntax Example | Use-case |
| Full module import | import utility | When many functions or classes from the module are needed |
| Selective function import | from utility import add | When only specific functions or classes are needed |
| Import with alias | from utility import subtract as sub | To handle name conflicts or simplify syntax |
| Wildcard import | from utility import * | Not recommended, but can be used for quick tests or very small scripts |
Further Considerations
- Module Initialization: Any code in the module being imported will run the first time it's imported. Be cautious about any side effects.
- Path Issues: If importing from a different folder, make sure that the folder is in Python’s search path, or use proper package structuring as explained above.
- Testing and Debugging: When breaking code into multiple files, remember to ensure each piece is tested individually to ensure module imports don’t introduce unexpected errors.
Using these methods effectively will allow you to structure your Python applications for better maintainability, readability, and scalability.

