What is Python path?
What is PYTHONPATH?
PYTHONPATH is an environment variable which the user can set to add additional directories that the user wants Python to add to the sys.path directory list. In short, we can say that it is an environment variable that you set before running the Python interpreter. Mostly you should not set these variables as they are not needed for Python to execute normal programs because it knows where its standard library is to be found. PYTHONPATH is used to help in importing the modules. So, when you import modules in your Python scripts, PYTHONPATH is also checked to see which directories might contain the imported module.
How to add to the PYTHONPATH in windows?
- My Computer > Properties > Advanced System Settings > Environment Variables >
- Click the “New” button in the top half of the dialog, to make a new user variable.
- Give the variable name as PYTHONPATH and the value is the path to the code directory.
- Click OK and then OK to save this variable.
To confirm PYTHONPATH, open Command Prompt and type :
- Echo %PYTHONPATH%
NOTE:- Don’t confuse it with Python PATH environment variable. That is used to assist OS in calling an executable from anywhere which means if you just type Python on your Command Window, the system will look into the PATH to see which directories might contain an executable named python.
Setting the path for Python:
Windows allows environment variables to be configured permanently at both User level as well as the System level, or temporarily in a command prompt.
To run Python properly from a Command Prompt, you might choose to change some default environment variables in windows.
- To temporarily set environment variables, open Command Prompt and use the “set” command:
C:\> set PATH=” Directory of your python folder in C: drive”; %path%
- To permanently modify the default environment variables:
How to set the Python path in windows:
- My Computer > Properties > Advanced System Settings > Environment Variables > Edit.
- Add python’s path to the end of the given directory ending with a semicolon (;)
How to set Python path in Unix or Linux:
To add the Python directory to the path for a particular session in Unix or Linux
- In the cshshell : type setenv PATH "$PATH:/usr/local/bin/python" and press Enter.
- In the bash shell (Linux) :type export PATH="$PATH:/usr/local/bin/python" and press Enter.
- In the sh or kshshell :type PATH="$PATH:/usr/local/bin/python" and press Enter.
NOTE:- /usr/local/bin/python is the path of the Python directory.