Import the function from the other Python file, then call it by name. For two sibling files, use from helpers import greet in main.py and call greet(). The filename, without its .py suffix, is the module name.
Import a function from a sibling file
When both files are in the same importable directory, Python can treat helpers.py as a module and make its function available to main.py.
project/
├── main.py
└── helpers.py
Define the function in helpers.py:
def greet(name):
return f"Hello, {name}!"
Import and call it from main.py:
from helpers import greet
print(greet("Ada"))
This prints Hello, Ada!. In the import statement, write helpers, not helpers.py, and make sure greet matches the function’s defined name.
Choose an import style
| Situation | Example | How to call it |
|---|---|---|
| Import one function directly | from helpers import greet |
greet("Ada") |
| Keep the module name visible | import helpers |
helpers.greet("Ada") |
| Import from a package | from my_app.helpers import greet |
greet("Ada") |
| Import from a sibling module inside a package | from .helpers import greet |
greet("Ada") |
The direct import is concise. The module-qualified form makes the function’s source clear and helps avoid name collisions. Prefer explicit imports over from helpers import *: a wildcard can bring in an unclear set of names and may overwrite names already in use.
#1 Best Overall
Import functions from a package
If the files are inside a package, use its dotted path for an absolute import, or a leading dot for a relative import from another module in the same package.
project/
└── my_app/
├── __init__.py
├── main.py
└── helpers.py
From my_app/main.py, either of these imports can access greet:
Rank #2
from .helpers import greet
# or
from my_app.helpers import greet
A relative import such as from .helpers import greet needs package context. Run the package from the project root with a module command such as python -m my_app.main. A file run directly as the top-level script does not have a package context for relative imports; use an absolute package import in that case, and launch it from a location where the package can be found.
Keep script startup code out of imports
Python executes a module’s top-level statements when it imports that module. Put code meant only to run when the file is launched directly behind a main guard:
def greet(name):
return f"Hello, {name}!"
if __name__ == "__main__":
print(greet("Ada"))
When this file is imported, the function definition is available but the guarded print does not run. When the file is run directly, the guarded block does run. This keeps reusable functions separate from script startup behavior.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Fix a ModuleNotFoundError
If Python cannot find the module, check the name, layout, and way the program is launched before changing the import path.
- Use the filename without
.pyin the import, and check spelling and capitalization. - For sibling files, confirm both are in the directory Python is searching. For package files, use the appropriate dotted or relative path.
- Launch the program from the intended project or package context. Python’s search path depends in part on the script location or current directory, as well as configured and installation paths.
- Check whether a local file has the same name as a standard-library or other intended module. The script directory can appear early in Python’s search path and cause an unintended file to be imported.
A dotted import is resolved through its parent packages as well as the requested module, so an error can indicate that a package component—not just the final function name—is unavailable. Avoid adding arbitrary sys.path edits as the first fix; make the project layout and launch command consistent with the import you intend to use.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Free tools Windows power users keep installed
One-click scans. No signup required.




