- Setting path for Python
- How to set python path in windows
- Using Python from a command windows?
- Python is not recognized..
- Setting Python Path in Unix or Linux
- What is PYTHONPATH?
- How to add to the PYTHONPATH in Windows?
- How to set python environment variable PYTHONPATH on Windows?
- Set PYTHONPATH using Command Prompt
- Set PYTHONPATH using PowerShell
- Set PYTHONPATH using the Windows Environment Variables dialog
- Set PYTHONPATH using an IDE or editor
- Set PYTHONPATH using a batch file
- Set python home path
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
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:
C:\>set PATH=C:\Program Files\Python 3.6;%PATH%
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 :
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.
- 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.
How to set python environment variable PYTHONPATH on Windows?
On Windows, you can set the PYTHONPATH environment variable to specify the directories that Python should search for modules when importing them. Here are several ways to set the PYTHONPATH environment variable on Windows
Set PYTHONPATH using Command Prompt
You can set the PYTHONPATH environment variable using Command Prompt by entering the following command −
$set PYTHONPATH=c:\path\to\my\modules
This sets the PYTHONPATH environment variable to c:\path\to\my\modules. To make this change permanent, you need to add it to the system environment variables −
Open the Start menu and search for «Environment Variables».
Click on «Edit the system environment variables».
Click on the «Environment Variables» button.
Under «System Variables», click on «New» to add a new environment variable.
Enter PYTHONPATH for the variable name and c:\path\to\my\modules for the variable value.
Click «OK» to save the environment variable.
Set PYTHONPATH using PowerShell
You can set the PYTHONPATH environment variable using PowerShell by entering the following command −
$env:PYTHONPATH = "c:\path\to\my\modules"
This sets the PYTHONPATH environment variable to c:\path\to\my\modules. To make this change permanent, you can add it to your PowerShell profile −
Open PowerShell and enter the following command to open your PowerShell profile −
Add the following line to the end of the file −
$env:PYTHONPATH = "c:\path\to\my\modules"
Save the file and close Notepad.
Set PYTHONPATH using the Windows Environment Variables dialog
You can also set the PYTHONPATH environment variable using the Windows Environment Variables dialog. Here’s how −
Open the Start menu and search for «Environment Variables».
Click on «Edit the system environment variables».
Click on the «Environment Variables» button.
Under «User Variables» or «System Variables», click on «New» to add a new environment variable.
Enter PYTHONPATH for the variable name and c:\path\to\my\modules for the variable value.
Click «OK» to save the environment variable.
Set PYTHONPATH using an IDE or editor
Many Python IDEs and text editors allow you to set environment variables directly from within the application. For example, in PyCharm, you can set the PYTHONPATH environment variable by going to Run > Edit Configurations and adding it to the environment variables section.
Set PYTHONPATH using a batch file
You can also create a batch file to set the PYTHONPATH environment variable. Here’s an example −
$@echo off $set PYTHONPATH=c:\path\to\my\modules $python my_script.py
This sets the PYTHONPATH environment variable to python modules
To set the PYTHONPATH environment variable using a batch file on Windows, follow these steps −
Open a text editor such as Notepad and create a new file.
Add the following line to the file −
$set PYTHONPATH=path\to\your\python\module.
Replace path\to\your\python\module with the actual path to the folder containing your Python module or package.
Save the file with a .bat extension, for example setpythonpath.bat.
You can now run this batch file to set the PYTHONPATH environment variable. To do so, open a Command Prompt window and navigate to the directory where the batch file is located. Then, type the name of the batch file and press Enter. The PYTHONPATH environment variable will be set for the current Command Prompt session.
If you want to set the PYTHONPATH environment variable permanently, you can add the batch file to your system’s startup folder. To do so, follow these steps −
Press the Windows key + R to open the Run dialog box.
Type shell:startup and press Enter.
This will open the startup folder for your user account. Copy the batch file you created earlier into this folder.
The batch file will now run automatically every time you start your computer, setting the PYTHONPATH environment variable for all Command Prompt sessions.
If you want to add multiple paths to your PYTHONPATH environment variable, you can separate them using semicolons (;). For example, if you have two folders containing Python modules or packages, you can set your PYTHONPATH environment variable to include both of them like this −
$set PYTHONPATH=path\to\your\first\python\module;path\to\your\second\python\module
This will add both folders to your PYTHONPATH environment variable, allowing Python to find modules or packages located in either of them.
It must be noted that if you have spaces in your path, you will need to enclose the entire path in quotes. For example −
$set PYTHONPATH="C:\Program Files\MyPythonModule";"D:\Python Projects"
This will add two paths to your PYTHONPATH environment variable, one of which contains a space in the path name.
Once you have set your PYTHONPATH environment variable, you can verify that it has been set correctly by opening a Command Prompt window and typing the following command −
This will display the current value of your PYTHONPATH environment variable. If you see the paths that you set earlier, then your PYTHONPATH environment variable has been set correctly.
To summarise, in this article, we have considered several different ways how the PYTHONPATH environment variable can be set on Windows with code examples
Set python home path
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter