Python - How To Change Python Path

ID : 531

viewed : 92

Tags : PythonPython Path

vote vote


Path variables are configured to contain the location of executable files for running commands. When we run a command in the command prompt, it notes the name of the command and tries to find the executable file in its path.

We can run different commands from the command prompt from Python. This is because the path variable for Python is also configured when it is installed. At times, however, we might want to add a new directory to the Path for maybe a new project to run this project directly from the command prompt.

In this tutorial, we will learn how to change or add PythonPath in windows.

Use the system settings to add or edit Python Path

In this method, we navigate to My Computer and select Properties. From there, we go to Advanced System Settings and select Environment Variables. From here, you can add the required directory to the Path of the already specified Python variable or create a new variable, assign its name as PythonPath and then mention the required full path. See the image below for more help.

set pythonpath

Use the command line method to add or edit Python Path

We can run the below command in the command prompt to achieve this.

set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib 

To set the PYTHONPATH permanently, add the line to your autoexec.bat.

Note that before using this method, run echo %PYTHONPATH%.

If this gives you a path, proceed with this method; otherwise, set the path as set PYTHONPATH=.;C:\My_python_lib.

If you don’t, windows will expand %PYTHONPATH% to an empty string as expected, it will keep it as %PYTHONPATH% in the PYTHONPATH, and everything will break.

  • Related HOW TO?