The quickest way to call a function from another file in Python is to keep both files in one folder, import the module by its file name without .py, and call the function. Import the whole module or just one name:
# pricing.py defines add_tax()
import pricing
print(pricing.add_tax(100))
from pricing import add_tax
print(add_tax(100))
I built each project below as a real folder and ran it in Command Prompt on Python 3.12.5.
In this tutorial, we’ll split code across three files, compare import styles, guard test code, use import * with __all__, build a package, and fix ModuleNotFoundError.
How do you call a function from another file in Python?
Put reusable functions in their own file. Here pricing.py does the math, cart.py imports it, and main.py imports both. If def is new to you, start with how to define a function in Python.
# pricing.py
TAX_RATE = 0.0825
def add_tax(amount):
return round(amount * (1 + TAX_RATE), 2)
def format_usd(amount):
return f"${amount:,.2f}"
# cart.py
from pricing import add_tax, format_usd
def cart_total(prices):
return format_usd(add_tax(sum(prices)))
Then main.py uses three import styles:
import pricing
from cart import cart_total
import pricing as pr
from pricing import TAX_RATE # copies the value
print(pricing.format_usd(pricing.add_tax(100)))
print(cart_total([19.99, 5.49, 42.00]))
pricing.TAX_RATE = 0.06
print(pr.add_tax(100), "| TAX_RATE copy:", TAX_RATE)
Output:
C:\pyguides\shop_basic> tree /f /a | findstr /v /c:"Folder PATH" /c:"Volume serial"
C:.
cart.py
main.py
pricing.py
No subfolders exist
C:\pyguides\shop_basic> python main.py
$108.25
$73.05
106.0 | TAX_RATE copy: 0.0825

main.py prints results from the other two.It works because Python searches your script’s folder first. For call basics, see how to call a function in Python.
Which import style should you use: import, from or as?
All three load one module object. import pricing shows where add_tax lives, from saves you typing, and as pr shortens long names.
The last line matters: changing pricing.TAX_RATE changed pr.add_tax(), but the from copy kept 0.0825. That’s why shared settings in a config module are read as module.NAME.
Stop the imported file from running code with the __name__ guard
Importing a file runs its top-level code. The test print() in discounts.py fires on every import; shipping.py guards its test:
# discounts.py
def member_price(price):
return round(price * 0.90, 2)
print("discounts self-test:", member_price(50.00))
# shipping.py
def ship_cost(weight_lb):
return 5.99 if weight_lb <= 2 else round(5.99 + (weight_lb - 2) * 1.25, 2)
if __name__ == "__main__":
print("shipping self-test:", ship_cost(6))
import discounts
import shipping
print("Member price:", discounts.member_price(80.00))
print("Shipping:", shipping.ship_cost(1.5))
print("shipping.__name__ is", repr(shipping.__name__))
Output:
C:\pyguides\shop_guard> python main.py
discounts self-test: 45.0
Member price: 72.0
Shipping: 5.99
shipping.__name__ is 'shipping'
C:\pyguides\shop_guard> python shipping.py
shipping self-test: 10.99

shipping.py starts directly.Imported, __name__ holds the module name; run directly, it’s "__main__". The same check powers a Python main() function with arguments.
Import all functions from a file with from module import *
from cart_math import * copies public names into your file. With __all__ defined, only the listed names come across, and names starting with an underscore are skipped either way:
# cart_math.py
__all__ = ["total", "average"]
def total(prices):
return round(sum(prices), 2)
def average(prices):
return round(sum(prices) / len(prices), 2)
def debug_dump(prices):
print(prices)
def total(prices):
return f"my own total() for {len(prices)} items"
print(total([4.99, 12.50]))
from cart_math import * # silently replaces my total()
print(total([4.99, 12.50]), average([4.99, 12.50]))
print("debug_dump" in dir())
Output:
C:\pyguides\shop_wild> python main.py
my own total() for 2 items
17.49 8.75
False

total(), and debug_dump stays out of __all__.That silent swap is the cost of import *. Each module has its own namespace, as accessing variables outside a function explains. I only use wildcards in the Python shell, and you should list names in real code.
Organize functions in a package folder with __init__.py
When your project grows, you group files in a folder. A folder with __init__.py is a package that can re-export names. For two scripts in one folder, importing a file from the same directory is enough.
# store/__init__.py
from .pricing import add_tax
from .inventory import stock_value
# store/inventory.py
from .pricing import format_usd
def stock_value(items):
return format_usd(sum(qty * price for qty, price in items))
# store/report.py
from .inventory import stock_value
print("Stock report:", stock_value([(3, 19.99)]))
store/pricing.py is the earlier file; utils/dates.py holds quarter_label(). Here’s main.py:
from datetime import date
from store import add_tax, stock_value
from store.pricing import TAX_RATE
from utils.dates import quarter_label # subfolder without __init__.py
print(add_tax(250.00), TAX_RATE)
print(stock_value([(3, 19.99), (10, 4.25)]))
print(quarter_label(date(2026, 10, 20)))
Output:
C:\pyguides\shop_pkg> tree /f /a | findstr /v /c:"Folder PATH" /c:"Volume serial"
C:.
| main.py
|
+---store
| inventory.py
| pricing.py
| report.py
| __init__.py
|
\---utils
dates.py
C:\pyguides\shop_pkg> python main.py
270.62 0.0825
$102.47
FY2026 Q4
C:\pyguides\shop_pkg> python store\report.py
Traceback (most recent call last):
File "C:\pyguides\shop_pkg\store\report.py", line 1, in <module>
from .inventory import stock_value
ImportError: attempted relative import with no known parent package
C:\pyguides\shop_pkg> python -m store.report
Stock report: $59.97

report.py works once it’s started with -m.utils has no __init__.py, but Python 3 still imports it as a namespace package.
Why do relative imports fail when you run the file directly?
The dot in from .inventory means “my own package.” If you run python store
eport.py, it has no parent package and fails; python -m store.report fixes that.
How do you fix ModuleNotFoundError when importing from another folder?
Python searches your script’s own folder, so scripts
eport.py can’t see the sibling billing folder. The fixed copy adds the project root to sys.path:
# scripts/report_fixed.py
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from billing.money import format_usd
print("Fixed with sys.path:", format_usd(48250))
import sys
print("sys.path[0] =", sys.path[0])
from billing.money import format_usd
print("October 2026 sales:", format_usd(48250))
Output:
C:\pyguides\shop_paths> tree /f /a | findstr /v /c:"Folder PATH" /c:"Volume serial"
C:.
+---billing
| money.py
|
\---scripts
report.py
report_fixed.py
C:\pyguides\shop_paths> python scripts\report.py
sys.path[0] = C:\pyguides\shop_paths\scripts
Traceback (most recent call last):
File "C:\pyguides\shop_paths\scripts\report.py", line 3, in <module>
from billing.money import format_usd
ModuleNotFoundError: No module named 'billing'
C:\pyguides\shop_paths> python -m scripts.report
sys.path[0] = C:\pyguides\shop_paths
October 2026 sales: $48,250.00
C:\pyguides\shop_paths> python scripts\report_fixed.py
Fixed with sys.path: $48,250.00

billing; python -m and sys.path both fix it.I prefer python -m because no path code ships with the script. For installed packages, see ModuleNotFoundError after pip install; for other layouts, see importing from a parent directory.
Ready for the next step? These guides build on the same functions:
- Import a class from a file in Python
- Call a function within a function in Python
- Send values back with the Python return statement
- Call a function by its string name
Frequently asked questions
Do both files need to be in the same folder?
No. Use a package and python -m, or extend sys.path.
How do I import all functions from a file in Python?
Use from module import *. You get the names in __all__, or every public name without it.
Does importing a file run all of its code?
Yes, once; later imports reuse the cached module. Guard test code with __name__.
What if two files define a function with the same name?
Import both modules and call a.report() and b.report().
How do I avoid circular imports?
Move shared functions into a third module, or import inside the function that needs it.
Where are Python imports documented?
See the official Python modules tutorial.

Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn