Skip to main content

How to Import a Module Inside a Function in Python

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
def <function>(<parameters>):
import <module>

return <module>.<name>(<parameters>)
PartWhat it means
import <module> inside the functionLoads 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​

lazy.py
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]).

Step 2: Run It​

Terminal
uv run python lazy.py
Output
statistics loaded before the call: False
5
statistics loaded after the call: True

The terminal after uv run python lazy.py: statistics loaded before the call: False, then 5, then 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 --help at 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​

MistakeWhat happensDo this instead
Using the module outside the function that imports itNameError: name 'statistics' is not defined: the name exists only inside the functionImport it where you use it, or at the top of the file
Expecting the import to run every time and slow every callOnly the first call loads the module; later calls find it in sys.modulesNothing 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​