Skip to content

import path

In Python, the import path refers to the path that Python searches to find the modules that you want to import into your code. This path includes the directories listed in the sys.path variable, which is initialized from the directory of the running script (or the current working directory in interactive mode), the PYTHONPATH environment variable, and installation-dependent defaults (including site-packages).

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:

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

You want to import it into a Python script:

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 March 10, 2026 • Reviewed by Dan Bader