What is the Python interpreter?

The Python interpreter, on a Windows environment, is a program that has been compiled into a Windows executable, which has the extension .exe. The Python interpreter, python.exe, has been written in C, an older and extensively used programming language with a more difficult syntax.

Programs written in C, which are also initially written as text files, must be converted into executables by a compiler, a specialized program that converts the text commands into machine code to create executable programs. This is a slow process that can make producing simple programs in C a laborious process. The benefit is that the programs produced are standalone programs capable of running without any dependencies. Python, on the other hand, interprets and executes the Python commands quickly, which makes it a great scripting language, but the scripts must be run through an interpreter and cannot be executed by themselves.

The Python interpreter, as its name implies, interprets commands contained within a Python script. When a Python script is run, or executed, the syntax is first checked to make sure that it conforms to the rules of Python (for example, indentation rules are followed and the variables follow naming conventions). Then, if the script is valid, the commands contained within are converted into bytecode, a specialized code that is executed by the bytecode interpreter, a virtual machine written in C. The bytecode interpreter further converts the bytecode (which is contained within files that end with the extension .pyc) into the correct machine code for the computer being used, and then the CPU executes the script. This is a complex process, which allows Python to maintain a semblance of simplicity.

There are other versions of the Python interpreter that have been written in Java (known as Jython) and in .NET (known as IronPython); these variants are used to write Python scripts in other computing environments and will not be addressed in this book. The ArcGIS installer includes the standard implementation of Python, which is also called CPython to distinguish it from these variants.

Where is the Python interpreter located?

The location of the Python interpreter within the folder structure of a computer is an important detail to master. Python is often downloaded directly from www.python.org and installed separately from ArcGIS. However, each ArcGIS version will require a specific version of Python; given this requirement, the inclusion of Python within the ArcGIS installation package is helpful. For this book, we will be using ArcGIS 10.2, and this will require Python 2.7.

On a Windows machine, the Python folder structure is placed directly on the C: drive, unless it is explicitly loaded on another drive. The installation process for ArcGIS 10.2 will create a folder at C:\Python27, which will contain another folder called either ArcGIS10.2 or ArcGIS10.2x64, depending on the operating system and the version of ArcGIS that has been installed. For this book, I will be using the 32-bit version of ArcGIS, so the final folder path will be at C:\Python27\ArcGIS10.2.

Within this folder are a number of subfolders, as well as python.exe (the Python interpreter). Also included is a second version of the interpreter called pythonw.exe. Pythonw.exe will execute a script without a terminal window with program feedback appearing. Both python.exe and pythonw.exe contain complete copies of all Python commands and can be used to execute a script.

Which Python interpreter should be used?

The general rule to execute a script directly using the Python interpreters is to use pythonw.exe, as no terminal window will appear. When there is a need to test code snippets, or to see the output within a terminal window, start python.exe by double-clicking on the executable.

When python.exe is started, a Python interpreter console will appear:

Which Python interpreter should be used?

Note the distinctive three chevrons (>>>) that appear below the header explaining version information. That is the Python prompt, where code is entered to be executed line by line, instead of in a completed script. This direct access to the interpreter is useful to test code snippets and understand syntax. A version of this interpreter, the Python Window, has been built into ArcMap and ArcCatalog since ArcGIS 10. It will be discussed more in later chapters.

How does the computer know where the interpreter is?

To be able to execute Python scripts directly (that is, to make the scripts run by double-clicking on them), the computer will also need to know where the interpreter sits within its folder structure. To accomplish this requires both administrative account access and advanced knowledge of how Windows searches for a program. We will have to adjust an environment variable within the advanced system settings dialogue to register the interpreter with the system path.

On a Windows 7 machine, click on the start menu and right-click on Computer, then select Properties from the menu. On a Windows 8 machine, click on Windows explorer and right click on This PC, and select Properties from the menu. These commands are shortcuts to get to the Control Panel's System and Security/System menus. Select Advanced system settings from the panel on the left. Click on the Environment Variables button at the bottom of the System Properties menu that appears. In the lower portion of the Environment Variables menu, scroll through the System variables window until the Path variable appears. Select it by clicking on it, and then clicking on the Edit button. The following window will appear:

How does the computer know where the interpreter is?

This variable has two components: Variable name (path) and Variable value. The value is a series of folder paths separated by semicolons. This is the path that is searched when Windows looks for specific executables that have been associated with a file extension. In our case, we will be adding the folder path that contains the Python interpreter. Type C:\Python27\ArcGIS10.2 (or the equivalent on your machine) into the Variable value field, making sure to separate it from the value before it with a semicolon. Click on OK to exit the Edit dialogue, and OK to exit the Environment Variables menu, and OK to exit the System Properties menu. The machine will now know where the Python interpreter is, as it will search all folders contained within the Path variable to look for an executable called Python. To test that the path adjustment worked correctly, open up a command window (Start menu/run cmd) and type python. The interpreter should directly run in the command window:

How does the computer know where the interpreter is?

If the Python header with version information and the triple chevron appears, the path adjustment has worked correctly.

Note

If there is no admin access available, there is a work around. In a command-line window, pass the entire path to the Python interpreter (C:\Python27\ArcGIS10.2\python.exe) to start the interpreter.