Now updated for Windows 10!
To access the Python interpreter and run Python scripts you need to know the location of python.exe for the installation you wish to use. To make running scripts easier you may wish to add this location to your system Path, either temporarily or permanently. This post describes how to do each of these as well as some general considerations.
If you are using Arcpy, you may also be interested in enabling Python installations that were not installed by ArcGIS to access Arcpy functionality.
If you are doing Python development, you may be interested in my Windows Dev Stack, which describes my development environment from high level technologies down to specific apps, and how they all work together.
Opening the command prompt
You can open the command prompt a few different ways:
- via the start menu (it is buried under Windows System)
- open the start menu and search “cmd”
- from Windows Explorer you can hold down shift and right-click on a folder, then select Open command window here
Once you are in the command prompt, you can change directories using ‘cd’ followed by the desired path to move into that directory, for example:
If you are using the command prompt a lot, check out my Windows Dev Stack, which introduces a much more user-friendly alternative to the default Windows command prompt!
Locating the Python installation
If you manually installed a version of Python it could be anywhere, but the default location is something like C:\PythonXX\, where XX is the major version; for example, 27 is the major version of Python 2.7.14.
ArcGIS 10.5 typically installs Python 2.7 to C:\Python27\ArcGIS10.5\, although it could be installed to another local drive such as D:. If you have installed 64-bit background geoprocessing (described here) on top of 10.5 it will install an additional Python, with a default location of C:\Python27\ArcGISx6410.5\
You can also try searching your hard drive for python.exe, but be aware that some applications might actually contain their own internal Python install (which you probably won’t want to use).
Accessing the interpreter and running scripts
To access the interpreter you can double click on python.exe.
Running scripts requires the script to be passed to python.exe (note that the .exe extension is not actually required to run the executable). In the most basic case this can be done from a command prompt running in the Python directory, for example:
would run test.py if it was also located in the Python directory.
However, it is much more useful to access Python from another location, such as the folder where you keep your scripts. To do this, use the full path to python.exe from a command prompt in the current location. For example, running test.py from a command prompt in D:\Projects\ (where the script is located) with Python installed at C:\Python27\ArcGIS10.5:
D:\Projects> C:\Python27\ArcGIS10.5\python test.py
To save on typing you might want to add python.exe to the system Path (this allows it to be accessed from a command prompt, no matter where you are, just by entering python, instead of the entire location to the executable). This means that the command above would become:
D:\Projects> python test.py
Adding Python permanently to the system path
The system path can be changed through the Environment Variables dialogue. WARNING: You could break your system if you are not careful here: don’t delete anything unnecessarily!
To bring up the Environment Variables dialogue in Windows 10:
- click on the Start Menu
- type (starts searching) “SystemPropertiesAdvanced”
- then click the Environment Variables button at the bottom
You have two options regarding the kind of variable you want to create:
- User variables: are applied to your login only (preferred option)
- System variables: will be applied for all users of the operating system
In either the User or System variables pane, select the ‘Path’ (or ‘PATH’) variable, then hit Edit. Click New and then paste the path to your Python installation folder – so, for my installation, I added this:
If there is no ‘Path’ variable under User variables you will need to create one by clicking New (set the name to Path and then you can paste or browse to the directory).
Temporarily adding Python to the system path
This is particularly handy for systems where you do not have administrator rights or cannot edit the path.
The command set lets you alter environment variables from the command prompt. The changes endure only for the instance of the command prompt from which they were invoked and end when it is exited. The best method is to add the path to your desired Python installation to the start of the Path, which you can do like this:
If you need to do this regularly you can create a batch script (a file with .bat as the extension). To do this: open Notepad, enter the command as above save the file as setPy.bat and run this within the command prompt before running scripts.
- If, when attempting to install an additional library to Python, you receive an error stating that the correct Python version is not found in the registry:
- Download the source of the library you are after (usually a .zip file, not specific to any Python version)
- Open a command prompt within the package folder and run:
python setup.py install
where python is the Python you want to install the library to. If it is not on the path (either temporarily or permanently) you must supply the full path to it.
- PYTHONPATH is an environment variable (like Path, described above) that is added to the list of places which Python will search for scripts, modules and libraries. It is not the path to or of the Python installation! If you wish to add folders to the PYTHONPATH you can follow the process described above, but editing the PYTHONPATH variable rather than Path.