Skip to content

import path

In Python, the import path refers to the list of locations (path entries) that Python’s path-based finder searches to find the modules you want to import. During an import, these locations usually come from the sys.path variable, which is initialized from the directory of the running script, the PYTHONPATH environment variable, and installation-dependent defaults (including site-packages). In interactive mode, the current working directory replaces the script’s directory. For subpackages, the search locations can also come from the parent package’s __path__ attribute.

Understanding and managing the import path is useful when working with large projects or when your modules are spread across different directories.

When you import a module, Python looks for it in the directories specified in the import path. If Python can’t find the module, you’ll encounter an ImportError exception. To solve this issue, you may need to adjust your import path by modifying sys.path or ensuring that your PYTHONPATH environment variable includes the directories where your modules reside.

Example

Here’s a quick example of how Python’s import path works. Suppose you have a module named module.py in a directory called project/:

project/
└── module.py

The module’s content looks like the following:

Language: Python Filename: module.py
def greet(name):
    return f"Hello, {name}!"

You want to import it into a Python script:

Language: Python
>>> import sys
>>> sys.path.append("/path/to/project")

>>> import module

>>> module.greet("Real Python")
'Hello, Real Python!'

In this example, sys.path.append("path/to/project") adds the directory containing module.py to the import path. This allows you to successfully import and use module.py in your code.

Tutorial

Python import: Advanced Techniques and Tips

The Python import system is as powerful as it is useful. In this in-depth tutorial, you'll learn how to harness this power to improve the structure and maintainability of your code.

intermediate python stdlib

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated July 8, 2026 • Reviewed by Dan Bader