Module Search Paths and sys.path
A module does not need to be in one special universal location. It needs to be in the same directory as the importing program or in one of the directories listed in sys.path.
Try it: What import Really Does
What happens on import: Python checks sys.modules, searches sys.path in order (first match wins), creates the module object, runs its top level once, then binds a name — the module, one function, or an alias — in the importing namespace; plus packages, __name__ == "__main__", ModuleNotFoundError and ImportError.
How it works
- If the name is already in sys.modules, reuse that module object: its code does not run again.
- Otherwise search each sys.path entry in order (the script's folder first); the first match wins, none raises ModuleNotFoundError.
- Create an empty module object, store it in sys.modules, then run the file's top level once (its __name__ is the module name, so a __main__ guard is skipped).
- import m binds m to the module; import m as x binds x; from m import f binds only f (ImportError if m has no f).
- A package runs its __init__.py first; submodules are found on the package's __path__ and become attributes of the package.
Default run (19 steps): $ python main.py — sys.path starts as [/project, /venv/site-packages, /usr/lib/python3.12]: the script's folder, then site-packages, then the standard library. Only our own modules are shown in sys.modules below. … Done. Printed: helpers loaded, __name__ = helpers | Hi Ann | Hi Ann.
Simplified: A fixed virtual project (main.py, helpers.py, pkg/, a site-packages folder and a one-file stand-in for the standard library). Built-in and frozen modules, .pyc caching, relative imports and namespace packages are not modelled; only our own modules are listed in sys.modules.
Loading the simulation…
The Import Location Problem
When a program imports a module, the module must be located where Python can search for it. Python uses sys.path for this purpose. The entries in sys.path are directory names representing locations from which modules are imported.
A module does not need to be in one special universal location. It needs to be in the same directory as the importing program or in one of the directories listed in sys.path.
Reading sys.path
sys.path is the list of directory names where modules are imported from. Each entry identifies a location that can be used when Python searches for an importable module.
The first string in sys.path is empty. According to the source material, this empty string indicates that the current directory is also part of sys.path. Because of that entry, a module located in the current directory can be imported directly.
Tracing Module Locations
A Module Beside the Importing Program
A program and a module are placed in the same directory. What does the module's location tell you about whether it can be imported?
- 1Locate the module
The module is in the same directory as the program that imports it.
- 2Relate the location to sys.path
The current directory is represented in
sys.pathby its empty first entry. - 3Determine availability
Because the current directory is part of
sys.path, the module is in a directory from which Python can import modules.
A module in the current directory can be imported directly.
The important relationship is location-based: sys.path lists directory names, and modules placed in those directories are in locations from which Python can import them. If a module is not in the current directory, it must be placed in one of the other directories listed in sys.path.
Using PYTHONPATH
The source material connects the empty first sys.path entry and the current directory with the PYTHONPATH environment variable. The practical idea is to reason from the directories available through the module search path: a module can be imported when it is in the current directory or in a directory listed in sys.path.
Mistakes with Module Placement
Common mistake Assuming that a module can be imported from any directory.
What it looks likeA module is stored in a directory that is neither the current directory nor one of the directories listed in sys.path.
Why it failsThe source states that modules should be placed in the same directory as the importing program or in a directory listed in
sys.path.Do this insteadPlace the module in the current directory or in one of the directories represented in sys.path.
Common mistake Treating the empty first sys.path entry as meaningless.
What it looks likeThe first entry is empty, so it is ignored when deciding whether a local module is available.
Why it failsThe empty string indicates that the current directory is part of
sys.path.Do this insteadInterpret the empty first entry as the current directory.
Common mistake Confusing the module's name with its location.
What it looks likeA module has the expected name, but its directory is not represented in sys.path.
Why it failsThe search path is defined by directory names, and the module must be in an available directory.
Do this insteadCheck the directory containing the module, not only the module's name.
Check Your Reasoning
A program imports a module. The module is not in the program's current directory. What must you check before deciding whether the module is available?
Hints
- Think about the list of directory names used for module imports.
- The module's directory must be represented in that list.
What do you think happens?
A module is placed in the current directory, and the first entry in sys.path is an empty string. Can the module be imported directly?
Reveal answer
Yes
The empty first entry indicates that the current directory is part of sys.path. A module in the current directory is therefore in a location from which Python can import modules.
Key Takeaways
- 1
sys.pathis the list of directory names from which modules are imported. - 2The empty first entry in
sys.pathindicates that the current directory is included. - 3A module should be in the same directory as the importing program or in a directory listed in
sys.path. - 4The module's location matters because Python searches locations represented by the module search path.
- 5PYTHONPATH is associated in the source material with the directories available through the module search path.
At a glance
sys.pathrecords directory names where modules are imported from.- Its empty first entry represents the current directory.
- Modules outside the current directory must be placed in a directory listed in
sys.path. - When an import is unavailable, inspect the module's directory and compare it with
sys.path.