python
PYTHONPATH
environment variables
programming
tutorial

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.

python
1import os
2
3pythonpath_value = os.environ.get("PYTHONPATH")
4print(pythonpath_value)

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.

python
1import sys
2
3for entry in sys.path:
4    print(entry)

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:

  • 'PYTHONPATH is unset.'
  • imports still work normally.
  • 'sys.path contains 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 ;.

python
1import os
2
3raw = os.environ.get("PYTHONPATH", "")
4entries = [part for part in raw.split(os.pathsep) if part]
5
6for entry in entries:
7    print(entry)

This works on Unix-like systems and Windows. Hardcoding separators is a common portability mistake.

When an import fails, it helps to print the interpreter executable, the environment variable, and the final path together.

python
1import os
2import sys
3
4print("Executable:", sys.executable)
5print("PYTHONPATH:", os.environ.get("PYTHONPATH"))
6print("sys.path:")
7for entry in sys.path:
8    print("  ", entry)

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:

python
1import sys
2
3print("Prefix:", sys.prefix)
4print("Base prefix:", sys.base_prefix)
5print("In venv:", sys.prefix != sys.base_prefix)

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.

bash
python -c "import os, sys; print(os.environ.get('PYTHONPATH')); print(sys.path)"

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.

python
1import site
2
3print(site.getsitepackages())
4print(site.getusersitepackages())

This does not replace sys.path, but it helps explain where third-party packages are expected to live.

Common Pitfalls

  • Looking only at PYTHONPATH when the real import behavior comes from sys.path.
  • Assuming PYTHONPATH must be set for imports to work at all.
  • Hardcoding : as the separator instead of using os.pathsep.
  • Forgetting that virtual environments and IDEs can change the active interpreter.
  • Debugging the shell environment when the problem is actually the wrong python executable.

Summary

  • Use os.environ.get("PYTHONPATH") to inspect the raw environment variable.
  • Use sys.path to see the actual module search path Python is using.
  • Do not assume those two values are identical.
  • Check sys.executable and virtual environment state when imports behave unexpectedly.
  • Use os.pathsep if you need to split PYTHONPATH into directories.

Course illustration
Course illustration

All Rights Reserved.