Setting path for Python
Windows allows environment variables to be configured permanently at both the User level and the System level, or temporarily in a command prompt. In order to run Python conveniently from a command prompt , you might consider changing some default environment variables in Windows.
To temporarily set environment variables , open Command Prompt and use the set command:
How to set python path in windows
To permanently modify the default environment variables :
My Computer > Properties > Advanced System Settings > Environment Variables > Edit
- Right-click 'My Computer'.
- Select 'Properties' at the bottom of the Context Menu.
- Select 'Advanced system settings'
- Click 'Environment Variables...' in the Advanced Tab
- Under 'System Variables': Click Edit
Add python's path to the end of the list (the paths are separated by semicolons(;))
Using Python from a command windows?
Open a command prompt window (press Windows+R, type in cmd, and hit enter).
Just type "python" on the command line and see if you get an error or not. If you see a response from a Python interpreter it will include a version number in its initial display.
Success, now you can start programming on Python .
Python is not recognized..
If you still get the Python is not recognized as an internal or external command, operable program or batch file error, there is something wrong with your Path variable settings . Moreover, you will have to reopen all command prompt windows in order for changes to the Path variable take effect.
Setting Python Path in Unix or Linux
To add the Python directory to the path for a particular session in Unix/Linux :
- /usr/local/bin/python is the default path of the Python directory.
What is PYTHONPATH?
PYTHONPATH is an environment variable which you can set to add additional directories that Python should add to the sys.path directory list. For most installations, you should not set these variables since they are not needed for Python to execute normal programs because Python knows where to find its standard library. PYTHONPATH is used to assist in import module lookup. So when you import modules in your Python scripts, PYTHONPATH is also looked into to check 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 OK again to save this variable.
In order to confirm PYTHONPATH , open a Command Prompt and type:
Now you can confirm the environment variable is correctly set.
- Don't confuse it with Python PATH environment variable. That is used to assist OS (Operating system) in invoking an executable from anywhere. Which means if you just type Python on your Command Window, system will look into PATH to see which directories might contain an executable named python.