PYTHONPATH Environment Variable in Python

Last Updated : 9 Jan, 2026

PYTHONPATH is an environment variable that tells Python where to look for modules and packages beyond the standard library and installed site-packages. It allows you to import user-defined or non-installed modules directly in your scripts, making it especially useful during development.

Suppose you defined a module as below:

Python
# user-defined module
# saved as my_module.py
def module_func():
    print("You just imported the user-defined module")

Now if you attempt to import the my_module.py in your python script as below:

Python
import my_module
my_module.module_func()

This will lead to the following error:

This is due to the reason that PYTHONPATH is not set yet. In simpler words, the Python interpreter cannot find the location of my_module.py file. So to set PYTHONPATH on a windowsmachine follow the below steps:

Setting PYTHONPATH on Windows

Step 1: Open your This PC (or My Computer) and right click on properties.

Step 2: After the properties window pop up click on Advanced System Settings.

Step 3: Now click on the environment variable button in the new popped up window as shown below.

Step 4: Now in the new Environment Variable dialog box click on New as shown below.

Step 5: Now in the variable dialog box add the name of the variable as PYTHONPATH and in value add the location to the module directory that you want python to check every time as shown below.

Step 6: Now open your command prompt and execute the my_script.py file with the below command:

python my_script.py

Now Python can locate your module and the script should run without errors.

Comment