General, Python

Locating Python and adding it to your system Path

To access the Python interpreter and run Python scripts you need to know the location of python.exe for your installation. 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.

Valid for Python 2.x

Please note that the content of this post has now been split into two separate posts: the information on enabling Python installations that were not installed by ArcGIS to access Arcpy functionality is now here.

Opening the command prompt

You can open a command prompt through the start menu (usually under Windows System), or by running cmd.exe from the run dialogue. To change directories use ‘cd’ followed by the desired path to change to that directory, for example:

C:>cd \test
C:\test>

To open a command prompt in a folder (for example, the folder where you keep your scripts) hold down shift, right click on the folder and select Open command prompt here.

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 represents the major Python version; for example, 26 is the major version of Python 2.6.5.

ArcGIS 10.0 typically installs Python 2.6 to C:\Python26\ArcGIS10.0\ while 10.1 installs Python 2.7 to C:\Python27\ArcGIS10.1\, although either could be installed to another local drive such as D:. If you have installed 64-bit background geoprocessing (described here) on top of 10.1 it will install an additional Python, with a default location of C:\Python27\ArcGISx6410.1\

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 required). In the most basic case this can be done from a command prompt running in the Python directory, for example:
C:\Python27\ArcGIS10.1>python test.py
would run test.py if test.py 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:\Python26\ArcGIS10.0:
D:\Projects>C:\Python26\ArcGIS10.0\python test.py

To save on typing it is preferable 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 calling python).

Adding Python permanently to the system path

The system path can be changed through the Environment Variables dialogue. WARNING: Don’t make any mistakes here (like deleting things), or your system might not work. Also make sure not to insert extra spaces anywhere.

To bring up the Environment Variables dialogue (Windows 7 and 8):

  • open Control Panel
  • go into System
  • click on Advanced system settings on the left
  • then click Environment Variables at the bottom

To bring up the Environment Variables dialogue (Windows 10):

  • open Start Menu
  • right click on the File Explorer icon, then into More then click Properties
  • click on Advanced system settings on the left
  • then click Environment Variables at the bottom

 

In the ‘System variables’ pane (at the bottom), scroll the box down and select the ‘Path’ variable, then go to Edit. Place the cursor in the box, hit End to get the cursor at the end, add a semi-colon (;) and then enter the path to your Python installation – so, for my installation, I added this:

;C:\Python27\ArcGIS10.1

Be careful not to insert any additional spaces before or after the path.

Temporarily adding Python to the system path

This is particularly handy for systems where you do not have administrator rights and cannot edit the system 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 the desired Python installation to the start of the system Path, like so:
set Path=C:\Python27\ArcGIS10.1;%Path%

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.

Miscellaneous notes

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)
  • Unzip
  • 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 searches 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, open the Environment Variables dialogue, as described above, click New, enter the name PYTHONPATH and then add folder paths as required. If adding multiple locations, they must be separated by a ;.

Advertisements

10 thoughts on “Locating Python and adding it to your system Path

  1. Bizarre, I was only discussing this issue yesterday with a work colleague and then your article came through my RSS feed! This is a really useful page. Thanks.

  2. I have tried everything to make my installation of Python use the ArcPy module. all I had to do was copy that file. WOW! Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s