Python Forum
[Basic] Set Windows Python Path to run python in any directory
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Basic] Set Windows Python Path to run python in any directory
#1
Quote:UPDATE: Since Python 3.5 the python installer usually has by default itself added to the PATH environmental variable. This whole process is no longer needed, assuming you just install 1 single python version and do not change the default install configuration. If you forget to check the box in the new installers, you can use this method, or you can reinstall python with PATH checkbox checked. IF you use multiple python versions you can use this method to name them accordingly to execute X versions without specifying each versions directory.

Adding python to the PATH environmental variable. This process will allow you to execute python from any directory and execute a python script anywhere. If you plan on just putting the py file in the installation path, then you can bypass this. 

In Windows 7 or less than, Click on Start Menu -> (on search) type: "command prompt". A Black icon "cmd" or Command prompt" will show up. Or open c:\Windows\system32\cmd.exe which will do the same. In windows 8, just type "cmd" in the menu, which should enable search, and pull of an icon of hte command console. This is a DOS prompt. You can do everything you can do normally on the PC in this DOS prompt with commands. The text before your cursor is the directory you are currently in. The command "cd" will change your current directory to the string after. The command 'dir' will list the directories in that directory that you are in. PythonXX should be in the directory C:\PythonXX (where XX represent the version you have downloaded). Once in the directory: type "python" to execute the interactive prompt.

Quote:UPDATE:
The default install path from python3.5 and on has been changed from 
C:\Python3.X
 
to 
C:\Users\username\AppData\Local\Programs\Python\Python3X-XX
Where Python35-32 would mean Python3.5 for 32 bit, etc. If you are using this or a later version change the path accordingly for this tutorial. Unless you changed the default path upon installation



C:\Windows\system32>cd c:\Python32

C:\Python32>python
Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 ...
Type "help", "copyright", "credits" or "license" for more information.
>>>
If you see ">>>", then you are in in the python interactive prompt.

However this will only execute python code in the directory C:\Python32. 

Are you getting this Error?
python is not recognized as an internal or external command,
operable program or batch file.
This will make the Python executable runable from anywhere, not any script. If the script lives in a different directory than where you are at, you still need to change to that directory, or give the full path of that file to execute it.
To make python execute in any directory in Windows. You have to add python to the PATH environmental variable. To get there: 
[Window 7] My Computer (right-Click) > Properties > Advanced System Settings > Environmental Variables > Under System Variables > double click Path > append python directory path to this, separated by semicolons (see below). Click OK to all the windows you have up.
[Windows 8] Go to Metro or Start and just type in "Advanced System Settings" > Environmental Variables > Under System Variables > double click Path > append python directory path to this, separated by semicolons (see below). Click OK to all the windows you have up.
Youtube  screencast

For the example above using Python32. In environmental variables there is a path varaible already existing
i changed the path variable from:
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\
to
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Python32
So i appended to the variable 
;C:\Python32
, the full path of Python32 separated with the other variables with a semi-colom

After that, OK out of all those windows, kill the command console and get a fresh command console with the same method you did before. However, this time, you will notice that you can change directory into any directory, and the command python will pull up the interpreter. An easy indicator that you were successful. 

an example output:
Quote:before setting the path:
C:\Users\metulburr>python
'python' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\metulburr>cd Python32

C:\Users\metulburr\Python32>python
Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

C:\Users\metulburr\Python32>cd ..

C:\Users\metulburr>python
'python' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\metulburr>
refresh the console
after setting the path:
C:\Users\metulburr>python
Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on wi
32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

C:\Users\metulburr>cd C:\

C:\>python
Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on wi
32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

C:\>

To do that in command prompt: 
set PATH=%PATH%;C:\My_python_lib
To add permanently add that path to autoexec.bat.

install/change PATHwith screenshots
https://www.howtogeek.com/197947/how-to-...n-windows/
Recommended Tutorials:


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python 3.9/3.8 and pip installation under Windows snippsat 4 140,745 Feb-17-2021, 01:58 PM
Last Post: snippsat
  [Part-2]Python environment Windows snippsat 4 25,149 May-24-2017, 10:44 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020