Change PythonPath

  1. Use the system settings to add or edit Python Path
  2. Use the command line method to add or edit Python Path

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, do e.g. 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.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python Path

  • Get Absolute Path in Python