Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
Adding a directory to the PYTHONPATH
environment variable in Windows allows Python to find additional modules and packages outside the default installation directories. To add a directory to the PYTHONPATH
in Windows, follow these steps:
Open the "Start" menu and search for "Environment Variables" or "Edit the system environment variables". Click on the "System Properties" option that appears in the search results.
In the "System Properties" window, click on the "Environment Variables" button near the bottom right corner.
In the "Environment Variables" window, you'll see two sections: "User variables" and "System variables". You can add the new PYTHONPATH
to either section, depending on whether you want the changes to apply only to the current user or to all users on the system.
Check if there's an existing PYTHONPATH
variable in the chosen section:
PYTHONPATH
variable, and click on the "Edit" button. In the "Edit environment variable" window, add a semicolon ;
(if not already present) at the end of the existing value, followed by the full path of the directory you want to add (e.g., C:\my_python_modules
). Click "OK" to save the changes.PYTHONPATH
as the "Variable name" and the full path of the directory you want to add (e.g., C:\my_python_modules
) as the "Variable value". Click "OK" to create the new variable.Click "OK" to close the "Environment Variables" window and then click "OK" to close the "System Properties" window.
Restart any open Command Prompt or PowerShell windows for the changes to take effect. Now, Python should be able to locate and import modules from the added directory.
Please note that these steps are for adding a directory to the PYTHONPATH
permanently. If you want to add a directory to the PYTHONPATH
temporarily for a single session, you can run the following command in Command Prompt or PowerShell:
$env:PYTHONPATH = "C:\my_python_modules;$env:PYTHONPATH"
Replace C:\my_python_modules
with the path of the directory you want to add.
Add directory to PYTHONPATH Windows:
set PYTHONPATH=%PYTHONPATH%;C:\path\to\your\directory
Setting PYTHONPATH environment variable in Windows:
[System.Environment]::SetEnvironmentVariable("PYTHONPATH", "C:\path\to\your\directory", [System.EnvironmentVariableTarget]::User)
How to modify PYTHONPATH in Windows:
import sys sys.path.append("C:\\path\\to\\your\\directory")
Adding a folder to Python path in Windows 10:
setx PYTHONPATH "%PYTHONPATH%;C:\path\to\your\directory"
Windows command prompt PYTHONPATH configuration:
set
command in command prompt:set PYTHONPATH=%PYTHONPATH%;C:\path\to\your\directory
Modifying user-specific PYTHONPATH in Windows:
[System.Environment]::SetEnvironmentVariable("PYTHONPATH", "C:\path\to\your\directory", [System.EnvironmentVariableTarget]::User)