How to Set Python Environment Variables
Environment variables in Python are mostly used to specify the file paths Python will use to search for modules and the locations of default system folders. The "putenv" command in the "os" module only allows you to change environment variables for Python processes created with the "subprocess" module. Even assigning values to the "os.environ" variable directly does not permanently affect your Python installation. Use the "Sytem" control panel in Windows to set Python environment variables that persist.
Instructions
-
-
1
Click the Start Menu and right-click "Computer". Choose "Properties" from the drop-down menu that appears.
-
2
Click "Advanced system settings" on the left side of the control panel that appears. Click "Environment Variables" in the next window that appears.
-
-
3
Click "New" in either the "User Variables" or "System Variables" section. If you choose the former, the environment variables you specify will only apply when you are logged in. Otherwise, the change will be system-wide.
-
4
Type the environment variable name into the "Variable name" field, and the value into the "Variable value" field. For example, use the "PYTHONPATH" variable to specify the file path(s) where modules are stored besides the 'Lib' directory in your installation folder. Consult the Python documentation at docs.python.org for a list of environment variables you can use. Click "OK" to commit the new variable.
-
5
Select a variable in either list and click "Edit" to change it. Multiple file paths may be separated with semicolons, so you can add a path to a variable instead of replacing its value entirely.
-
6
Launch the Python command-line interpreter, and type the following commands to verify that the environment variable has been set:
import os
os.environ['PYTHONPATH']
-
1