How do I find out my PYTHONPATH using Python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When people ask for "the Python path," they usually mean one of two things: the PYTHONPATH environment variable or the actual module search path stored in sys.path. Those are related, but they are not the same object. If you are debugging imports, sys.path is usually the more useful answer.
Read The PYTHONPATH Environment Variable
If you want the raw environment setting, read it like any other process variable.
If the result is None, the variable is not set for the current Python process. That is normal on many systems. Python does not require PYTHONPATH to exist.
Inspect The Real Import Search Path
The interpreter builds a final search list at startup. That final list is stored in sys.path.
This output usually includes the current script directory, site-packages locations, standard library directories, and any paths injected from PYTHONPATH. For import debugging, this is the runtime truth.
Why PYTHONPATH And sys.path Differ
PYTHONPATH is only one input to sys.path. Python also adds directories based on how the interpreter was launched, whether a virtual environment is active, and what site configuration is installed.
That means the following can all be true at once:
- '
PYTHONPATHis unset.' - imports still work normally.
- '
sys.pathcontains many directories.'
So if the question is "what will Python search when I import a module," use sys.path. If the question is "what did my shell export," use os.environ.
Split The Variable Into Individual Entries
On most systems, PYTHONPATH contains several directories joined by the platform path separator. Use os.pathsep instead of hardcoding : or ;.
This works on Unix-like systems and Windows. Hardcoding separators is a common portability mistake.
Print A Useful Import Debug Report
When an import fails, it helps to print the interpreter executable, the environment variable, and the final path together.
This small diagnostic often reveals the real issue immediately. For example, you may discover that the wrong Python executable is running or that a virtual environment is not active.
Virtual Environments Change The Answer
If you use venv, virtualenv, Poetry, Conda, or an IDE-managed interpreter, sys.path can differ substantially from your shell expectations. A virtual environment may add its own site-packages directory even when PYTHONPATH is empty.
You can verify that quickly:
If sys.prefix and sys.base_prefix differ, you are almost certainly inside a virtual environment.
Command-Line Shortcuts
If you do not want to create a script file, you can print the same information directly from the command line.
That is useful for comparing interpreters such as python, python3, and a project-specific virtual environment executable.
The site Module Can Help Too
For installed package locations, the site module exposes additional details.
This does not replace sys.path, but it helps explain where third-party packages are expected to live.
Common Pitfalls
- Looking only at
PYTHONPATHwhen the real import behavior comes fromsys.path. - Assuming
PYTHONPATHmust be set for imports to work at all. - Hardcoding
:as the separator instead of usingos.pathsep. - Forgetting that virtual environments and IDEs can change the active interpreter.
- Debugging the shell environment when the problem is actually the wrong
pythonexecutable.
Summary
- Use
os.environ.get("PYTHONPATH")to inspect the raw environment variable. - Use
sys.pathto see the actual module search path Python is using. - Do not assume those two values are identical.
- Check
sys.executableand virtual environment state when imports behave unexpectedly. - Use
os.pathsepif you need to splitPYTHONPATHinto directories.

