October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Call a Function from Another File in Python

Import a function from another Python file with its module name, then call it directly or through the module. Learn package imports and common fixes for ModuleNotFoundError.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.Support on Ko-Fi

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 .py in 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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.