What is PYTHONPATH?

PYTHONPATH is an environment variable that tells Python about extra folders to search when you import a module. Python normally looks in the current folder and the standard library first, and then in the folders listed in PYTHONPATH.

Set it on macOS or Linux like this:

export PYTHONPATH=/home/robin/mylibs

On Windows (Command Prompt):

set PYTHONPATH=C:\mylibs

You can check the full list of search folders from Python itself.

import sys
print(sys.path)

Output (your computer will show different folders):

Tutorials Class - Output Window

[”, ‘/home/robin/mylibs’, ‘/usr/lib/python3.12’, …]

Your own folder appears in the list. This is useful when you keep shared code outside the project, but a virtual environment and proper packages are usually a cleaner way.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.