Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to set your python path on Windows?
The PYTHONPATH environment variable tells Python where to look for modules and packages beyond the standard library. While modifying PYTHONPATH is rarely needed, here's how to set it on Windows when necessary.
Accessing Environment Variables
To set the PYTHONPATH on Windows, navigate to the system environment variables ?
My Computer > Properties > Advanced System Settings > Environment Variables
Alternatively, you can press Win + R, type sysdm.cpl, and press Enter to access System Properties directly.
Setting the PYTHONPATH Variable
In the Environment Variables dialog, look for PYTHONPATH under System Variables. If it doesn't exist, click "New" to create it. If it exists, select it and click "Edit".
Add your directory path to the end of the current value, separating multiple paths with semicolons ?
C:\Python39\Lib\site-packages;C:\MyProjects\CustomModules
Example
To add a custom module directory, append it to the existing PYTHONPATH ?
Original: C:\Python39;C:\Python39\Scripts Updated: C:\Python39;C:\Python39\Scripts;C:\MyCustomModules
Verifying Your PYTHONPATH
After setting the environment variable, open a new Command Prompt and verify the path ?
import sys
for path in sys.path:
print(path)
Your custom directory should appear in the output.
Best Practices
In most cases, you should avoid modifying PYTHONPATH. Instead, consider these alternatives:
- Use
pip installto install packages properly - Create virtual environments with
venv - Use
sys.path.append()in your script for temporary additions - Install your package in development mode with
pip install -e .
Conclusion
While you can set PYTHONPATH through Windows Environment Variables, it's generally better to use proper package installation methods. Only modify PYTHONPATH when you have a specific need that can't be solved with standard Python packaging tools.
