How to Import a Module Inside a Function in Python
Imports usually sit at the top of a file, but Python lets you write one inside a function too. The module then loads the first time the function runs, not when the file starts. That's useful when a module is slow to load and only some runs need it. This guide shows it on Python 3.15.2.
Syntax
def <function>(<parameters>):
import <module>
return <module>.<name>(<parameters>)
| Part | What it means |
|---|---|
import <module> inside the function | Loads the module when this line runs, the first time the function is called |
<module>.<name> | Uses the module, but only inside the function: the name is local to it |
The Python tutorial says this is allowed: "It is customary but not required to place all import statements at the beginning of a module".
The Example
An average function imports the statistics module only when it runs. The script checks sys.modules, Python's list of modules loaded so far, before and after calling it.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
import sys
def average(numbers):
import statistics
return statistics.mean(numbers)
print("statistics loaded before the call:", "statistics" in sys.modules)
print(average([2, 4, 9]))
print("statistics loaded after the call:", "statistics" in sys.modules)
![The lazy.py script in a macOS-style code window: an average function that imports statistics inside its body and returns statistics.mean, with prints of whether statistics is in sys.modules before and after calling average([2, 4, 9]).](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/programming/python/2026/python-import-inside-01-code.webp)
Step 2: Run It
uv run python lazy.py
statistics loaded before the call: False
5
statistics loaded after the call: True

Before the call, statistics wasn't loaded. Calling average([2, 4, 9]) ran the import, returned the mean 5, and left the module in sys.modules. The command works unchanged in PowerShell.
A second call doesn't load the module again: the import system documentation says "The first place checked during import search is sys.modules. This mapping serves as a cache of all modules that have been previously imported". After the first time, the import line only looks the module up.
When to Do It
- A slow import that only some runs need. A command-line tool can answer
--helpat once, and load a big library only when it really has work to do. - Not as a habit. Imports at the top show at a glance what a file depends on, so keep them there unless there's a reason.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Using the module outside the function that imports it | NameError: name 'statistics' is not defined: the name exists only inside the function | Import it where you use it, or at the top of the file |
| Expecting the import to run every time and slow every call | Only the first call loads the module; later calls find it in sys.modules | Nothing to fix |
FAQ
Is importing inside a function slower?
Only the first call pays for loading the module. Each later call does a quick lookup in sys.modules.
Does the module's code run twice if two functions import it?
No. It runs once, the first time anything imports it, and both functions then share the same module.
Conclusion
The import statistics line inside average loaded the module only when the function ran: sys.modules said False before the call and True after it.
Additional Resources
- Modules — the Python tutorial on importing.
- The module cache — how
sys.modulesmakes repeated imports cheap.
