How to set python environment variable PYTHONPATH on Windows?


On Windows, you can set the PYTHONPATH environment variable to specify the directories that Python should search for modules when importing them. Here are several ways to set the PYTHONPATH environment variable on Windows

Set PYTHONPATH using Command Prompt

You can set the PYTHONPATH environment variable using Command Prompt by entering the following command −

$set PYTHONPATH=c:\path\to\my\modules

This sets the PYTHONPATH environment variable to c:\path\to\my\modules. To make this change permanent, you need to add it to the system environment variables −

Open the Start menu and search for "Environment Variables".

Click on "Edit the system environment variables".

Click on the "Environment Variables" button.

Under "System Variables", click on "New" to add a new environment variable.

Enter PYTHONPATH for the variable name and c:\path\to\my\modules for the variable value.

Click "OK" to save the environment variable.

Set PYTHONPATH using PowerShell

You can set the PYTHONPATH environment variable using PowerShell by entering the following command −

$env:PYTHONPATH = "c:\path\to\my\modules"

This sets the PYTHONPATH environment variable to c:\path\to\my\modules. To make this change permanent, you can add it to your PowerShell profile −

Open PowerShell and enter the following command to open your PowerShell profile −

$notepad $PROFILE

Add the following line to the end of the file −

$env:PYTHONPATH = "c:\path\to\my\modules"

Save the file and close Notepad.

Set PYTHONPATH using the Windows Environment Variables dialog

You can also set the PYTHONPATH environment variable using the Windows Environment Variables dialog. Here's how −

Open the Start menu and search for "Environment Variables".

Click on "Edit the system environment variables".

Click on the "Environment Variables" button.

Under "User Variables" or "System Variables", click on "New" to add a new environment variable.

Enter PYTHONPATH for the variable name and c:\path\to\my\modules for the variable value.

Click "OK" to save the environment variable.

Set PYTHONPATH using an IDE or editor

Many Python IDEs and text editors allow you to set environment variables directly from within the application. For example, in PyCharm, you can set the PYTHONPATH environment variable by going to Run > Edit Configurations and adding it to the environment variables section.

Set PYTHONPATH using a batch file

You can also create a batch file to set the PYTHONPATH environment variable. Here's an example −

$@echo off
$set PYTHONPATH=c:\path\to\my\modules
$python my_script.py

This sets the PYTHONPATH environment variable to python modules

To set the PYTHONPATH environment variable using a batch file on Windows, follow these steps −

Open a text editor such as Notepad and create a new file.

Add the following line to the file −

$set PYTHONPATH=path\to\your\python\module.

Replace path\to\your\python\module with the actual path to the folder containing your Python module or package.

Save the file with a .bat extension, for example setpythonpath.bat.

You can now run this batch file to set the PYTHONPATH environment variable. To do so, open a Command Prompt window and navigate to the directory where the batch file is located. Then, type the name of the batch file and press Enter. The PYTHONPATH environment variable will be set for the current Command Prompt session.

If you want to set the PYTHONPATH environment variable permanently, you can add the batch file to your system's startup folder. To do so, follow these steps −

Press the Windows key + R to open the Run dialog box.

Type shell:startup and press Enter.

This will open the startup folder for your user account. Copy the batch file you created earlier into this folder.

The batch file will now run automatically every time you start your computer, setting the PYTHONPATH environment variable for all Command Prompt sessions.

If you want to add multiple paths to your PYTHONPATH environment variable, you can separate them using semicolons (;). For example, if you have two folders containing Python modules or packages, you can set your PYTHONPATH environment variable to include both of them like this −

$set
PYTHONPATH=path\to\your\first\python\module;path\to\your\second\python\module

This will add both folders to your PYTHONPATH environment variable, allowing Python to find modules or packages located in either of them.

It must be noted that if you have spaces in your path, you will need to enclose the entire path in quotes. For example −

$set PYTHONPATH="C:\Program Files\MyPythonModule";"D:\Python Projects"

This will add two paths to your PYTHONPATH environment variable, one of which contains a space in the path name.

Once you have set your PYTHONPATH environment variable, you can verify that it has been set correctly by opening a Command Prompt window and typing the following command −

$echo %PYTHONPATH%

This will display the current value of your PYTHONPATH environment variable. If you see the paths that you set earlier, then your PYTHONPATH environment variable has been set correctly.

To summarise, in this article, we have considered several different ways how the PYTHONPATH environment variable can be set on Windows with code examples

Updated on: 02-May-2023

28K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements