Chapter 2 Working with Python editors

2.1 Introduction

This chapter introduces the use of Python editors, or integrated development environments (IDEs), to write and test your scripts. Many different IDEs are used by the Python community, and experienced developers often have a strong personal preference for a specific editor. Although the choice of which IDE to use is important, Python code is the same regardless of which editor is used.

This chapter explains the different ways you can run Python code, and introduces several different IDEs, including IDLE, PyCharm, and Spyder. The Python window in ArcGIS Pro presents another way to write Python code directly within ArcGIS Pro without having to work in a separate application.

2.2 Using a Python editor or IDE

A computer script is essentially a list of commands that can be run by a program or scripting engine. Scripts are usually plain text documents that have a specific file extension and contain instructions written in a scripting language. Most scripts can be opened and edited using a basic text editor. However, using a specialized script editor to work with a script provides additional functionality when you’re writing the script and allows you to run the script as well.

You can work with Python in many ways. The most basic approach is to use the so-called command line or command prompt. If you have used other programming languages, you may be familiar with this type of interface. To access the Python command line in Windows, search for the application called Python Command Prompt. This application resides in the ArcGIS program group.

Python command line in Windows.

On a typical computer, this is not the only Python command prompt. For example, if you installed ArcGIS for Desktop 10.x, you will also have a command prompt in the ArcGIS program group called Python (command line) which works with Python 2.7. If you installed other programs that use Python, you may have additional command prompts. When working with ArcGIS Pro, you should use the specific command prompt that is installed with the software, which ensures you have the correct version of Python that works with ArcGIS Pro and that you have access to the necessary packages.

Notice that the first line in the command prompt starts with (arcgispro-py3), which is referred to as the specific Python environment. An environment includes a specific version of Python and any packages that are installed. Different projects may require different environments. This scenario can be tricky, especially if you are working on multiple computers and different platforms. The solution in the Python community is to use virtual environments, which allow you to set up and use different configurations on the same computer. The default environment that is configured as part of the installation of ArcGIS Pro is called arcgispro-py3. As the name suggests, this environment is specific to using ArcGIS Pro and Python 3.

Following the environment is the full path where the specific environment is installed on your local hard drive. This path will vary depending on how ArcGIS Pro is installed. The path is the default working directory when running Python; in many cases, you will want to change it to a custom directory of your choice.

Although this interface provides full access to all of Python’s functionality, it provides limited support for writing and testing scripts. So instead of using the command prompt interface, it is typically more productive to use a Python editor. A Python editor has a menu-driven interface and tools for organizing and testing Python scripts to make working with Python easier.

A Python editor is also referred to as an integrated development environment (IDE). The terms “Python editor” and “Python IDE” are often used interchangeably. There are many different IDEs, including several open-source and commercial packages. Some IDEs are developed for specific platforms, such as Windows, macOS, or Linux, whereas others are designed to work with one or more specific programming languages, such as Java or a .NET language.

A comprehensive list of Python editors can be found on the Python wiki page (http://wiki.python.org/moin/PythonEditors). The editor you use is largely a matter of preference, and experienced Python programmers have their favorites. Python syntax remains the same for different editors, so you don’t need different scripts to work with different editors.

The three IDEs discussed in the rest of this chapter are IDLE, Spyder, and PyCharm. These editors were chosen because they represent a range of different functionality, from very basic to relatively sophisticated. However, all the code in this book can be used by any IDE that is properly configured to work with ArcGIS Pro.

One more Python editor deserves mention here: Jupyter Notebook. Jupyter Notebook is an open-source web application in which you can write and run Python code. Although Jupyter Notebook does not have the same functionality as a more “traditional” IDE, it provides an intuitive and interactive way to write and run code. Some would say Jupyter Notebook is a more modern and exciting way to develop code. Jupyter Notebook is popular for applications in data science and visualization. Jupyter Notebook is often used to write code that employs the ArcGIS API for Python. The ArcGIS API for Python is a Python package for working directly with web GIS independent of ArcGIS Pro. The ArcGIS API for Python is not covered in this book, but in the book Advanced Python Scripting for ArcGIS Pro.

2.3 Using IDLE

The default IDE that comes with any installation of Python is the Integrated DeveLopment Environment (IDLE). Notice how the L is capitalized. IDE is the general term used for Python editors, whereas IDLE is the specific editor that is provided as the default application to work with Python scripts. IDLE is basic, but because it is the default editor that is installed with Python, it is good to become familiar with it. The screen captures of Python code you saw in chapter 1 used IDLE. Any computer that has Python installed will have IDLE installed, so you can count on it being available. IDLE is also often referred to as the Python Shell. “Shell” is a general computing term to describe a user interface, as opposed to the command line interface.

To access IDLE, type the idle command in the Python command prompt. You type it at the blinking cursor at the end of the line following the > symbol.

Typing the idle command at the Python command prompt.

Then press the Enter key. This command brings up IDLE.

The Python Shell interface.

Notice how the name of the application at the top is Python 3.6.9 Shell, but it is commonly referred to as IDLE, and you will see this reference in some of the menu options. Because you started IDLE from the arcgispro-py3 environment, the version of Python being used is the one that installs with ArcGIS Pro. At the time of writing, it is version 3.6.9 for ArcGIS Pro 2.5, but this will change with future releases of the software.

Because it is somewhat inconvenient to launch IDLE by first starting the command prompt, you can also create a shortcut for IDLE on your desktop or add it to the ArcGIS Pro program group. The path to run IDLE is as follows for a typical installation of ArcGIS Pro:

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\idlelib\idle.bat

Although no program shortcut is created for IDLE, there is a built-in shortcut from within File Explorer. Navigate to a folder in which one or more of your Python scripts are located. When you right-click on a Python script—i.e., a file with a .py extension—it brings up various options to work with the file, including Edit with IDLE (ArcGIS Pro). This option appears only if ArcGIS Pro is installed on your local computer.

Built-in shortcut for using IDLE to open existing scripts.

If you have another installation of Python or another Python editor, there may be additional options. For example, if you have ArcMap 10.x installed, there will also be an option called Edit with IDLE. This opens the version of IDLE that is installed with ArcMap 10.x—i.e., Python 2.7.16 when using ArcMap 10.7.1.

Clicking Edit with IDLE (ArcGIS Pro) opens the correct version of IDLE to work with ArcGIS Pro. The script opens in a script window. To launch the Shell, click Run > Python Shell.

This built-in shortcut is helpful because it uses the active environment in ArcGIS Pro. By default, this is the arcgispro-py3 environment, but if the ArcGIS Pro application is using a different environment, that environment will be used when launching IDLE in this manner. This feature is helpful because other shortcuts (e.g., entries in the program group or desktop shortcuts) are specific to one environment only. However, from within IDLE, it is not immediately clear which environment is being used. You must open ArcGIS Pro and check the active environment.

Alternatively, to check the environment being used from within IDLE, run the following code in the Python Shell:

>>> import numpy
>>> print(numpy.__file__)

Note that there are two underscores before and after file. This code prints the location where the files for one of the packages reside—in this example, the NumPy package. This location indirectly tells you the environment being used. When using the default environment, the result looks something like this:

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages \numpy\__init__.py

In IDLE, the result is as shown in the figure.

Python code in IDLE to check the location of the file of the NumPy package.

The result confirms that the environment being used is arcgispro-py3.

Although IDLE is installed as part of the ArcGIS Pro installation, no program shortcut is created because there is a different version of IDLE for every environment. Therefore, be careful when using any shortcuts you create for IDLE because they are specific to one environment.

To add this shortcut in Windows, right-click on your desktop, and click New > Shortcut. In the dialog box, enter the path to run IDLE. If you are not sure that is the correct location, you can also browse to the file.

Creating a desktop shortcut for IDLE.

Click Next, and in the next dialog box, enter a meaningful name such as IDLE or IDLE for Pro. This step creates a shortcut with a generic icon by default.

Generic icon for IDLE.

You can replace the generic icon with the actual IDLE icon. Right-click on the shortcut, click Properties, and on the Shortcut tab, click Change Icon. Navigate to the idle.ico file, which is typically located in the following location:

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\idlelib\Icons\idle.ico.

This file replaces the icon with the actual IDLE icon.

Specific icon for IDLE.

You can now double-click the icon to start IDLE.

Every Python installation comes with its own version of IDLE. If you have ArcGIS Desktop 10.x installed, you typically will have a shortcut under the ArcGIS program group called IDLE (Python GUI). If you are using ArcMap 10.7.1, and you start IDLE using this shortcut, you will see that it uses Python 2.7.16. To work with ArcGIS Pro, you should use only the version of IDLE that is installed with ArcGIS Pro in the location referenced previously. Using this version of IDLE ensures that you have the correct version of Python and have access to the packages needed to work with ArcGIS Pro.

Descriptions of the various menu items of IDLE can be obtained from the help menu. On the Python Shell menu bar, click Help > IDLE Help to view these descriptions. More information on IDLE can be found at http://www.python.org/idle.

Notice that the last line in the Python Shell window starts with the symbol >>>. This notation is the prompt of the interactive Python interpreter. This prompt signals where you can type code and press Enter, and the interactive interpreter carries out your command.

Are you ready for your first line of Python? At the prompt in IDLE, type the following text after the prompt:

>>> print("Hello World")

When you press Enter, the following output appears:

Hello World
>>>

What’s happening here? When you press Enter, the interactive Python interpreter reads the input command, printing Hello World to the next line, and gives you a new prompt on the following line, waiting for the next input. The term printing here refers to writing text to the screen, not sending a file to an external printer.

You have now seen why Python is called an interpreted programming language. When you finish typing your commands and press Enter, these commands are interpreted and immediately carried out.

You will also notice something else about the interactive Python interpreter. As soon as you type some text, it is given a color based on the nature of the input. For example, as soon as you type the word print, it turns purple. Similarly, the string "Hello World" is green. And the print output is blue.

Coloring of interpreted code elements.

This coloring is a way for the interactive Python interpreter to show how the elements of the code are being interpreted. In this case, purple is for functions, green is for strings, and blue is for output. This syntax highlighting is a helpful means of learning about code and error checking as you write it. Be aware, however, that syntax highlighting conventions vary between Python editors, so don’t get too used to a specific color scheme.

What if you type something else that does not make sense to the interactive Python interpreter? For example:

>>> I like eggs and bacon for breakfast.

You get immediate feedback that the syntax is invalid:

SyntaxError: invalid syntax
>>>

When you are learning the basics of Python syntax, it is useful to work directly in the interactive Python interpreter. You get immediate results, and you can keep going with new lines of code without worrying about saving your work. However, when you are ready to write slightly more complex code that consists of multiple lines, it is more beneficial to write it as a script file you can save. Remember that code written to the interactive Python interpreter is generally not meant to be saved—although you can copy it to a script and continue it there.

Next, you will see how writing a script differs from writing code in the interactive Python interpreter. On the Python Shell menu bar, click File > New File. This command opens a new window named Untitled. This is a script window, and there is no prompt.

Opening a new script window in IDLE.

Now type the same line of code as before:

print("Hello World")

Notice that the same syntax highlighting shows up. When you press Enter, however, the cursor jumps to the next line, but otherwise nothing happens. This inactivity is because a script itself is not interactive. A script must be run as a program for the commands to be executed. Before you can run a script, however, it must be saved. On the menu bar, click File > Save As, and save your script as hello.py. The file extension .py indicates it is a Python script.

Contents of hello.py script file.

Now it is time to run the script. On the menu bar, click Run > Run Module. The string Hello World is printed to the interactive Python interpreter.

Text printed to the interactive Python interpreter.

When you are coding, it is useful to have both the interactive Python interpreter and your script(s) open at the same time. If you want to try out something quickly or check the syntax of a line of code, you can use the interactive Python interpreter. The script window, on the other hand, contains the actual lines of code you want to save and keep working on. You can test your script by producing results that are printed to the interactive Python interpreter.

IDLE is a good basic editor, and most experienced Python programmers are familiar with it. It is, however, somewhat limited in its functionality, and most programmers use at least one other IDE—sometimes several. The other IDEs used in this book include Spyder and PyCharm; these IDEs are reviewed next. For the remainder of this book, it is assumed that you have access to IDLE and either Spyder or PyCharm. The syntax of your Python code, however, is the same, independent of the editor. The major differences between editors lie in how Python scripts are created, organized, and tested, but the actual code is the same.

2.4 Using Spyder as your Python IDE

Spyder is short for the Scientific Python Development Environment. As you can guess from the name, it is widely used in scientific computing. Spyder is not installed and configured by default as part of the ArcGIS Pro installation. However, Spyder can be installed as a Python package. A package, in this case, refers to functionality created by a third party that can be added to your Python installation. A package is sometimes also referred to as a library, although that term is used a bit loosely in Python. To make it easier to manage and install Python packages, the Python community uses package managers. ArcGIS Pro integrates a widely used package manager called conda.

The easiest way to use conda is from within ArcGIS Pro. Start ArcGIS Pro, enter your login credential if necessary, and start a new project. Click Project from the top menu, and then click Python. This brings up the Python Package Manager.

Python Package Manager in ArcGIS Pro.

The Python Package Manager shows which packages are currently installed, and allows you to update and add packages. Notice how the default environment is arcgispro-py3. To install Spyder, you must add a package. However, starting with ArcGIS Pro 2.2, the default environment arcgispro-py3 cannot be modified. What this means is that you must create a clone of the default environment, and then install Spyder for this new environment.

To clone the default environment, click the Manage Environments button, which brings up the Manage Environments dialog box.

Manage Environments dialog box.

Unless you have previously created environments, the only environment showing is the default arcgispro-py3 environment. When you click the Clone Default button, the default environment starts cloning but without a prompt to specify the name or location. When you click the Clone button next to an environment, it brings up the Clone Environment dialog box where you can enter the name and location of the new environment.

Clone Environment dialog box.

By default, the location for a cloned environment is in the user profile: C:\Users\<Your Name>\AppData\Local\Esri\conda\envs. You can choose a different name and location, but the defaults are typically OK.

Click the Clone button to start the process of cloning the default environment. Next, the cloned environment will appear in the Manage Environments dialog box.

Cloned environment in the Manage Environments dialog box.

Click on this environment to activate it, and click Close. You must restart ArcGIS Pro for the changes to take effect. Once you restart ArcGIS Pro and return to the Python Package Manager, the Project Environment is now arcgispro-py3-clone.

New cloned environment is the active environment.

Now you can install Spyder. Click Add Packages, and then you can search for or scroll to view the package called spyder.

Adding the spyder package.

Click on the spyder package in the list of packages, and then click the Install button. In the Install Package dialog box, the package manager shows you which other packages Spyder depends on. Managing these dependencies is one of the benefits of using a package manager such as conda.

Install Package dialog box for Spyder.

Confirm that you agree to the terms and conditions, and click Install. You may receive a warning message from your operating system that new software will be installed.

Once the installation is complete, you can check that the spyder package is now listed under the installed packages. You will also notice that the number of packages installed for the active environment has increased because the installation of Spyder adds more than 20 packages.

Once Spyder is installed, you can launch it by typing the spyder command in the Python command prompt, but this command works only if you use the new environment where the package was installed. Because this process can be a bit cumbersome, it is easier to create a shortcut for the application. The Spyder application resides in the following folder, provided you used the default name and location:

C:\Users\<Your Name>\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\Scripts \spyder.exe

Note that there is also a spyder3.exe file, but that is not the correct one. There is also an icon file called spyer.ico, but this file is just an image, not the application itself.

Once you have created the shortcut, it uses a generic icon.

Generic Spyder icon.

You can replace this icon with the actual Spyder icon. Right-click on the shortcut, click Properties, and on the Shortcut tab, click the Change icon. Navigate to the spyder.ico file, located in the same folder as the spyder.exe file.

Spyder icon.

You can now use the shortcut to launch the Spyder application.

The Spyder interface initially looks a bit more complex compared with IDLE, but it has some of the same general elements.

Spyder interface.

In the left panel is the script window; by default it is a script named temp.py. In the upper right is a panel with options to look at variables and files. In the lower right is a panel called the IPython console, which is like the interactive interpreter in IDLE.

Try running the same code as before in the IPython console.

In [1]: print("Hello world")

When you type this line of code and press Enter, the result prints to the next line, and the console starts a new entry for the next line of code.

Running code in the IPython console in Spyder.

The IPython console is similar to the interactive interpreter in IDLE, but the input prompt >>> is replaced by In [1], and the number increases with every prompt. Earlier versions of Spyder used a “regular” console, which also employed the >>> prompt, but the most recent version for Python 3 uses the IPython console, which includes a numbered input prompt.

IPython provides a more versatile alternative to the regular interactive interpreter, but for our purposes, we won’t be using much of this additional functionality from within Spyder. For the purpose of the code examples in this book, therefore, it does not really matter whether you use the >>> prompt or the In [n] prompt.

You can also run your code as a script. Remove the existing lines of code from the temporary script, and enter your code on the first line. Click File > Save As, and save your hello.py script in a folder of your choice. You can see your saved files by browsing to your working folder using the navigation tools on the right side of the main toolbar in Spyder. When you click the File explorer tab, you can see the file you just saved.

You can run your script by clicking the Run file icon Run file icon in Spyder..

The result prints to the IPython console. The prompt shows which file was run, followed by the print result on the next line.

Results from the script printed to the IPython console in Spyder.

As you can tell from this example, Spyder and IDLE have a slightly different approach to running Python code, but the actual code is identical, and so is the result.

2.5 Using PyCharm as your Python IDE

PyCharm is the third and final IDE discussed here. PyCharm is developed by a company called JetBrains. PyCharm has a lot of features for professional developers, many of which we won’t need at this point, but it is a popular IDE among Python programmers. There is both a paid professional edition and a free community edition; the latter will suffice for this book. Although specifically targeted to Python, PyCharm also supports several other programming languages, including JavaScript and HTML.

PyCharm is not available as a Python package but must be downloaded and installed separately. You can download the software from https://www.jetbrains.com/pycharm. Once you download and install the Windows version of the community edition, some configuration is required. As you will recall, both IDLE and Spyder were installed as part of ArcGIS Pro or by using the Python Package Manager, respectively. This installation ensures the editor uses the correct environment. This is not the case for PyCharm, which is installed separately from ArcGIS Pro.

The installation adds a desktop shortcut, but you can also add it manually using the following file, although the version number will change over time:

C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.4\bin\pycharm64.exe

The desktop shortcut is as shown:

PyCharm desktop shortcut

The following steps outline how to configure PyCharm, assuming PyCharm was installed and launched with the initial default settings. From the File menu, click Other Settings > Settings for New Projects. Click the Project Interpreter option in the left panel. Ignore the existing options in the drop-down menu, and instead click on the cog icon to the right of the Project Interpreter drop-down arrow.

Selecting a Project Interpreter in PyCharm.

Click the Add option to bring up the Add Python Interpreter dialog box. Click the Conda Environment option on the left side. Click the Existing environment option. For the interpreter, click on the triple-dot icon to the right of the Interpreter drop-down arrow, and navigate to the arcgispro-py3 environment as follows:

C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe

Also check the option to make this environment available to all projects. This option makes it easier to use the same environment for future projects.

Completed settings for the Add Python Interpreter dialog box.

After the interpreter is set, the list of packages that are part of the arcgispro-py3 environment are shown, which is the same as those shown in the Python Package Manager in ArcGIS Pro. However, the environment should not be managed from within PyCharm, but by using conda through the Python Package Manager.

Once these settings are applied, you may need to restart PyCharm for the changes to take effect. After PyCharm launches again, you can create a new project in a folder of your choice. To make the interactive interpreter visible, click Tools > Python Console, which brings up a prompt like the ones in IDLE and Spyder.

PyCharm interface with the Python Console.

Notice how the Python console shows the correct version of Python (3.6.9). You can now use this console as an interactive interpreter by typing a line of code and pressing Enter. Also notice how PyCharm uses the same In [n] prompt used in Spyder instead of the >>> prompt in IDLE.

Running code in the Python console in Python.

To write your code in a script file, in the Project panel to the left, right-click on your working folder, and click New Python File. After you give it a name, your script is saved to your working folder and shows up in the panel to the right. You can write your script, and then run it by right-clicking inside the script and clicking Run. You can also run a script by right-clicking inside the script in the Project pane and clicking Run, or from the Run menu, click Run.

The result is printed to a new window below the script. This is not the same Python console from earlier, but a new Run window that is specific to the results from running the current script. Regardless of how the results are presented, the Python code and the results are identical to the earlier examples using IDLE and Spyder.

Results from the script printed to the Run window in PyCharm.

PyCharm has a lot of different options and features that make it a relatively sophisticated IDE. The user interface can also be intimidating for the beginning programmer. If you are just getting started with programming, IDLE is a good place to start, but you are encouraged to try Spyder and PyCharm to become familiar with a different interface and learn some additional functionality. Spyder has a slight edge because it installs as a Python package and requires less configuration. On the other hand, PyCharm makes it easier to run multiple environments, which is explained in section 2.14.

The examples in this book use IDLE as the primary Python editor, with some references to Spyder and PyCharm to illustrate functionality not available in IDLE. Choosing a Python editor is largely up to you, but you are encouraged to try several to determine which one you find easier to use. You may also benefit from talking to colleagues or classmates about which IDE they use. The code examples in this book can be used in any IDE that is compatible with Python 3, provided it is configured to use the correct ArcGIS Pro environment.

2.6 Notes on other IDEs

In addition to dedicated Python editors such as Spyder and PyCharm, general-purpose code editors can also be used for writing Python scripts. These editors usually offer syntax highlighting, formatting options, and other options for working with code. Examples of widely used code editors include Microsoft Visual Studio and Eclipse. Typically, these code editors work with multiple programming languages. As a result, experienced code developers may use a single editor to work with multiple languages. For example, Visual Studio is primarily intended to develop applications that run on the .NET framework using C# but also can be used to work with Python. Similarly, Eclipse is widely used to work with Java, but by adding the PyDev module, you can use it to work with Python.

It should be noted that editors such as Visual Studio and Eclipse have a lot of functionality that the beginning Python coder does not need. To make the learning curve a bit less steep, if you are just getting started with Python, you are encouraged to use a Python-dedicated editor such as IDLE, Spyder, or PyCharm. Dedicated Python editors have some advantages over other editors, including the fact that writing Python scripts requires little custom configuration.

2.7 Using the Python window

In addition to using a Python editor, you can also work with Python directly from within ArcGIS Pro by using the Python window. To open the Python window in ArcGIS Pro, click on the Analysis tab and then on the Python window icon.

Python window icon in ArcGIS Pro.

In the Python window, the top section is called the “transcript,” and the bottom section is called the Python “prompt.” The transcript is initially blank. It provides a record of previously entered Python code and its results.

Empty Python window without any code.

You can type your line of code in the Python prompt. Notice that it is called a prompt, even though there is no >>> symbol.

Python window with a single line of code typed in the prompt.

Once the code is typed, press Enter to execute the code. The line of code is copied to the transcript, and the result appears in the next line in the transcript.

Python window with the line of code and the results appearing in the transcript.

The Python window allows for rapid testing of relatively simple lines of code, but there is no separate script window. The Python window is therefore equivalent to the interactive interpreter in IDLE and the consoles in Spyder and PyCharm, but it lacks the full functionality of an IDE.

2.8 Important notes about Python scripts and sample code in this book

Python scripts, by definition, are modules. Modules are Python’s highest level of organizing code. The actual file that contains the Python script is a simple text file, and therefore any text editor can be used to write a script. The file extension for a Python script is .py, which is automatically associated with a Python editor. So even though a Python script has the file extension .py, the content is plain text without any formatting.

When you use a Python editor, the code comes alive in the form of syntax highlighting and other functionality, as the previous examples illustrate. This functionality makes it much easier to review existing code and write new lines of code.

Be careful using basic text editors, such as Notepad, to write Python scripts. Text editors are not designed specifically for writing code. They do not preserve the proper formatting of a script and do not provide syntax highlighting or any other tools that help with writing properly formatted scripts. Make sure your editor is specifically designed for working with code such as the example IDEs discussed in this chapter.

Python script names must start with a letter and can be followed by any number of letters, digits, or underscores (_). Script names should not use Python keywords. Chapter 4 covers the Python keywords.

Whenever sample code in the book is preceded by the prompt (>>>), the code is being written in an interactive interpreter such as the Python window in ArcGIS Pro or the interactive window in IDLE. When you press Enter, the line of code is executed immediately. An example of what code preceded by the prompt looks like follows:

>>> print("Hello World")

As the earlier examples have shown, not all editors use the same >>> symbol. For example, the IPython console in Spyder uses the notation In [n], where n represents a line number. The Python window in ArcGIS Pro does not use a symbol at all. As a result, the >>> symbol is used simply to indicate that the code should be run interactively, regardless of which editor is used and which symbol, if any, precedes it.

Whenever sample code is not preceded by the prompt, as shown, the code is being written to a script window. The code must be saved to a script file with a .py extension, and then it must be run for the code to be executed.

print("Hello World")

The results of running a script are printed to the interactive window. Most of the sample code in this book, however, can be run in both an interactive interpreter and as part of a script.

2.9 Writing and running code in the Python window

The Python window is a quick and convenient way to write short Python code to run geoprocessing tools while taking advantage of the full functionality of Python. It also provides a great way for beginners to learn Python.

The Python window can be used to run one or more lines of Python code. It is a useful place to experiment with syntax and work with short snippets of code. The term snippet is used in programing to indicate a small portion of code, typically only a few lines long. Scripting ideas can be tested within the Python window before using the code in a larger stand-alone script. The availability of Python functionality directly within ArcGIS Pro also provides an efficient mechanism for accessing and executing geoprocessing tasks.

The Python window is a pane in ArcGIS Pro, similar to the Geoprocessing or Catalog pane. Panes can be resized, moved, and docked as needed. Although it is a pane within the terminology of ArcGIS Pro, everyone calls it the “Python window.”

Python window in ArcGIS Pro.

The bottom section of the Python window is the interactive Python interpreter, also called the Python prompt, or “input section.” This is where you enter Python code line by line. Notice how the default message that shows is “Enter Python code here,” but this message disappears once you start using the Python window. There is no command prompt (>>>) as there is in some other interactive interpreters.

The top section of the Python window is called the transcript, or “output section.” When Python code is entered in the prompt and the line of code is run, the transcript section shows this code followed by the result. The transcript provides a record of all previously entered Python code, which is where the name comes from.

The split between the transcript and prompt sections can be a bit counterintuitive at first because it differs from other interactive interpreters such as IDLE. In IDLE, you enter a line of code, press Enter, and the result is printed to the next line in the same window. On the other hand, the split between input and output is common in other user interfaces, and it is relatively easy to get used to.

Python code is entered one line at a time in the prompt section of the Python window. After pressing Enter, the code is executed, and the result is displayed immediately in the transcript section. Consider the following example.

Enter this line of code in the prompt:

x = 7

This code assigns the value of 7 to the variable x.

Line of code in the Python window.

When you press Enter at the end of the line of code, the code is executed. The line of code now appears in the transcript section.

Executed code appears in the transcript section of the Python window.

Next, enter the following line of code:

x * 3

Executing this code carries out a simple calculation. When you enter the code and press Enter, the code appears in the transcript section followed by the result of the calculation on the next line.

Executed code and results appear in the transcript section of the Python window.

You can keep entering code in this manner to execute a longer workflow.

To clear the transcript, right-click inside the transcript section, and click Clear Transcript. Keep in mind that when you clear the transcript, any variables you used in your code keep their value in the current session. All the previously executed code also is kept in memory, even if it is no longer showing in the Python window. When you close the Python window, all the code is also kept in memory, and the contents of the transcript stay the same. When you save your project, the contents of the transcript and all the code you used are saved. This feature allows you to return to working on your code where you left off.

In some cases, a single line of code cannot be executed by itself. Consider the following code example:

x = 17
if x == 17:
print("Correct")

This example assigns a value to a variable called x, and then checks the value. In a real-world scenario, the value for x could be provided from an outside source such as a user. The if statement signifies the beginning of a block of code, and as a result, the next line must be indented. The if statement is part of a multiline construct—there must be at least one additional line of code for it to be executable. In other words, the second line of code cannot be run by itself.

The Python window recognizes this construct, so when you enter the second line of code and press Enter, the code is not run but instead the prompt jumps to the next line.

Start of a block of code following the if statement.

The Python window also adds indentation to the next line of code. When you type the next line of code, it is automatically indented because the if statement is always followed by a block of code with indentation.

You can now proceed with entering the third and final line of code.

Block of code following the if statement.

By default, the Python window does not assume the multiline construct is finished, and you can continue writing the block of code without the code being executed. When you press Enter, the cursor jumps to the next line of indented code.

New indented line of code.

When you are finished writing the block of code and want to run it, simply press Enter at the prompt without entering any new code. This step runs the block of code, and the code is shown in the transcript, including any results.

Executed code and results appear in the transcript section of the Python window.

When a line of code is not executed but instead jumps to the next line of code, it is also referred to as a secondary prompt. Secondary prompts make it possible to complete several lines of code before running them all at once. The Python window recognizes multiline constructs automatically, such as the if statement example. This usage is consistent with the functionality of other Python IDEs.

All the core functionality of Python, as well as all the geoprocessing tools in ArcGIS Pro, can be used in the Python window. In programming terminology, this means the functionality is exposed. In addition, one of the advantages of working in the Python window is that you can interact with layers in your map.

An example of the use of a geoprocessing tool in the Python window follows. The Get Count tool determines the number of rows for a feature class or table. In this example, the feature class is obtained from a layer in the current map, and the result of the Get Count tool is printed.

Results of running the Get Count tool on a data layer.

The layer called rivers is in the current map and therefore can be called directly by its name. You can also reference feature classes and other datasets on disk; chapter 5 covers this. Again, don’t worry about the syntax at this point. The basic idea is that the Python window can be used to call any of the available geoprocessing tools. You can test code, practice Python syntax, and save your code, all from within ArcGIS Pro.

When you execute a line of code, the prompt section of the Python window is disabled, and you will see an X to the upper left of the prompt and a series of dots moving across to indicate that the code is running, as shown in the figure.

Example of code running in the Python window.

Typically, this indicator only shows very briefly, but if the X remains visible and you wish to cancel, you can click the X to stop the code.

Initially, it may appear that running a tool from the Python window takes more effort compared with using the regular tool dialog boxes. However, there are many advantages to using the Python window:

Some of these advantages are explored in the following sections, whereas others are covered in later chapters.

2.10 Getting assistance in the Python window

When working in the Python window, it is common to make small mistakes. Consider the following example in which the name of a data layer is typed incorrectly.

Example error message in the Python window.
Description

Error 000732: Input Rows: Dataset river does not exist or is not supported. Failed to execute (GetCount).

The layer river does not exist in the current map—it should have been rivers. Several messages are reported to indicate that the tool did not execute because of invalid parameters. You can click on the error code to link to the ArcGIS help pages with a detailed explanation of the error.

Instead of typing the line of code again, you can use the up arrow and down arrow keys to access previously entered code. You can simply bring up the previous line of code and make the change.

With your cursor inside the prompt, press the up arrow to bring up the previous line of code.

Bringing up the previous lines of code using the Up Arrow.

Then make the correction and execute the code.

Results of running the corrected code.

The up arrow and down arrow keys limit the amount of retyping needed when you want to access previous lines of code.

The need for typing can also be minimized by using autocompletion prompts. For example, when you start typing arcpy.Get, you are shown a drop-down list of all the ArcPy functions that start with Get. You can choose from the list by clicking on an item, and the prompt is populated with the item.

Example of autocompletion prompts in the Python window.

This feature greatly reduces the need to memorize the exact names of geoprocessing tools and other functions, limits the number of typos, and is much faster than typing the full name.

The Python window autocompletion prompts also include aids for setting parameter values. For example, when you start entering tool parameters and an input feature class is expected, you are prompted with a list of available layers, such as the example shown in the figure.

Example of parameter prompts in the Python window.
Description

Error 000732: Input Rows: Dataset river does not exist or is not supported. Failed to execute (GetCount).

In this case, the map contains three data layers, each of which make for a meaningful input parameter for the Get Count tool. You choose the layer of interest, and the prompt is populated with the name of the layer. The resulting code automatically includes the correct syntax—in this case, the use of quotation marks, or “quotes,” around the name of the layer and parentheses around the parameter.

Example of complete and correct code.
Description

count = arcpy.management.GetCount(‘rivers’).

Prompts in the Python window also extend to variables used in earlier lines of code. For example, when you start typing print(co, you are prompted by a list of elements, starting with the letters co, that could logically be used. In the example, this includes the variable count, which was assigned a value in the previous line.

Example of autocompletion prompts in the Python window.
Description

Autocompletion prompts include compile(), complex(), ConnectionAborted Error(), and so on up to count.

Another useful aspect of autocompletion is assistance with syntax. For example, when you start typing print(, the Python window automatically adds the closing parenthesis at the end, even before you have finished typing what comes after the opening parenthesis.

Making effective use of these prompts limits the amount of typing necessary and reduces typos. The prompts also remind you of the proper syntax, so they can help you learn how to write Python code.

Another example of the assistance provided by the Python window is the display of the general tool syntax. When the exact name of the tool is entered in the prompt, a pop-up window shows the tool syntax, as illustrated.

Example of tool syntax in a pop-up window.

This pop-up window shows the parameters of a specific tool and explains each parameter. Hovering over the blue info icon brings up an explanation of the tool, and clicking on the icon links to the online help page for the tool.

In the case of the Get Count tool, there is only a single parameter, and the syntax is intuitive. However, for some tools that you have not used often, the syntax may not be as intuitive. Some tools also have a substantial number of parameters, making it more difficult to write the code without looking up the syntax.

The example shown in the figure illustrates a much less commonly used tool with many parameters. Having the syntax available in the prompt makes it easier to write the correct syntax.

Example of the syntax of a tool with many parameters.

The pop-up window consists of two sections. The top section shows the syntax of the tool, whereas the bottom section shows an explanation of the active parameter. This active parameter updates as you are writing, and the explanation updates accordingly.

The Python window is fully integrated into the geoprocessing framework. For example, any outputs from a geoprocessing tool that are run in the Python window are added to the current map. In addition, any tools you run in the Python window appear in the geoprocessing history, which is accessed from the History pane.

The example in the figure shows how the Buffer tool was run from the Python window using a data layer from the current map as the input. The resulting geodatabase feature class is added to the current map, and the tool is added to the geoprocessing history.

Results from running a tool in the Python window are added to the current map, and the tool is added to the geoprocessing history.

Another way to save on typing is to drag things directly into the Python window. With the Python window open, you can drag many elements from other panes in ArcGIS Pro, including tools from the Geoprocessing pane, one or more data layers from the Contents pane, and even tools you have already run from the History pane. The example in the figure shows what happens when you drag the previously run tool from the geoprocessing history into the prompt section of the Python window.

Code in the Python window after dragging the Buffer entry from the geoprocessing history into the prompt.
Description

Transcript section shows the code for running the Buffer tool, with only the first three parameters. Prompt section shows the code from the history, with all parameters filled out.

The syntax may look somewhat different, but the tool parameters are, in fact, identical. Chapter 5 provides details about working with tool parameters in Python.

Being able to drag geoprocessing history entries into the Python window is a great way to learn about Python syntax. You can run a geoprocessing tool using the regular tool dialog box, and then drag the code snippet from the History pane into the Python window to see the syntax.

2.11 Exploring Python window options

Several other options are available by right-clicking in the Python window. In the transcript section, you can select one or more lines, right-click on them, and then click Copy. You can also right-click in the transcript section, and then click the Select All option before copying. This can be useful if you want to copy the code into another file, such as a script, in a different IDE. There are also options to clear the transcript or save the entire transcript.

The prompt section contains right-click options to Copy, Cut, Paste, and Select All. In addition, Clear Window removes any code from the prompt section, and Load Code allows you to bring in existing code from a Python file.

Of these options, Save Transcript and Load Code are some of the most useful. The following sections discuss these features in more detail.

2.12 Saving your work in the Python window

The Python window is great for running relatively short sections of Python code. You can experiment with Python statements and get immediate results. Single lines of code also can be run a second time. The Python window, however, is not intended to create longer and more complex Python code. For this, you need Python scripts.

To use the code that you have already written in the Python window in another editor, save the contents by right-clicking in the Python window and then clicking Save Transcript. The contents of the Python window can be saved as a Python file (.py) that you can then open in your preferred IDE.

Consider the example in the figure from earlier sections.

Example code in the transcript section of the Python window.
Description

count = arcpy.management.GetCount(‘points’)

print(count)

22

Saving the transcript to a Python file makes it possible to open the script in a different Python editor—in this case, IDLE, as shown in the figure.

Contents of the getcount.py script in IDLE.

The script includes all the lines of code from the transcript section of the Python window, whereas any results are shown as comments preceded with the # symbol. The first line of code also provides a comment on the use of UTF-8 coding. This line of code means the code uses 8-bit blocks to represent a character, which is one of several ways to encode characters. Chapter 4 provides more information on encoding.

You can now edit your script in your Python editor of choice.

2.13 Loading code into the Python window

The Python window is an interactive interpreter and, in general, you can use it for the quick execution of a few short lines of code. However, you can also load code into the Python window that has already been written and saved as a file. You can copy code from a Python editor or text editor and paste it into the Python window. You can also use the Load Code option to import the complete contents of a script.

Consider the script that was previously exported (i.e., getcount.py). Starting off with an empty prompt section in the Python window, right-click and click Load Code. Then choose a Python script file, such as the getcount.py script.

Contents of the getcount.py script loaded into the Python window.

When the existing script is loaded, notice that all the lines are added to the prompt section, including any comments. The lines of code from the script are not run one by one as they are loaded. This allows you to preview all the lines of code and make any necessary changes before running the code.

When you run the code by pressing Enter, the code is executed and appears in the transcript, again including any comments.

Results of running the script loaded into the Python window.

Normally you would not want to run lengthy scripts from within the Python window, but loading blocks of existing code saves time compared with typing or copying and pasting individual lines of code.

Being able to save your work as scripts, load existing code from scripts, and copy and paste code from existing scripts makes it easier to go back and forth between the Python window and a Python editor. Ultimately, you will typically want to save your final code as a script file for future use.

2.14 Using IDEs for ArcGIS Desktop 10.x

Although this book is focused strictly on the use of Python for ArcGIS Pro, many users who are still using ArcGIS Desktop 10.x may find themselves running scripts for both versions. Running both versions can present a challenge because you are using two different versions of Python. The two versions can exist side by side on a single computer, but you will need to adjust how you use your IDE. This section explains how to configure your IDE so you can run both versions.

First, using IDLE for ArcGIS Desktop 10.x is straightforward because it is installed as a shortcut with the typical installation. You can launch IDLE by searching for the application called IDLE (Python GUI). This application resides in the ArcGIS program group. Running this application brings up the Python Shell.

Python Shell for Python 2.7.16.

This version of IDLE runs Python version 2.7.16 and cannot be used to run scripts that are written for Python 3 for use with ArcGIS Pro. The interface, however, is nearly identical to the Python 3.6.9 Shell. ArcGIS for Desktop 10.x uses only a single environment, so there is no need to worry about different versions of IDLE.

Spyder also can be used as an IDE to work with scripts in Python 2.7. However, the installation and configuration are somewhat cumbersome. In short, you need to use conda to create a new environment for Python 2.7, and then install Python as a package for that environment. The procedure requires the use of conda using command line, which is not covered in this book. As an alternative, you can use a different package manager such as PIP directly from command line in Python 2.7 to add the Spyder package. These steps are not explained here, but the Spyder website has installation instructions for using PIP.

PyCharm is an excellent choice to work with both versions of Python because the configuration is relatively easy. A single installation of PyCharm can use multiple interpreters. To configure PyCharm for this purpose, click File > Other Settings > Settings for New Projects > Project Interpreter. Ignore the existing options in the drop-down menu, and instead click on the cog icon and then Add… . In the Add Python Interpreter dialog box, click System Interpreter on the left side. PyCharm will likely have recognized the interpreters that are installed as part of a typical ArcGIS installation, including C:\Python27\ArcGIS10.7\python.exe. If this interpreter is not recognized, you can navigate to the file manually.

Adding an interpreter for Python 2.7 in PyCharm.

Once the system interpreter has been selected, it will show up as the project interpreter. The list of packages for this interpreter is also shown. Click OK to apply the project interpreter. You may need to restart your computer for the settings to take effect. When you start a new project in PyCharm, you have the option to choose one of the preconfigured interpreters. In the New Project dialog box, click the Existing interpreter option. You now have the option to use the Python 2.7 interpreter for ArcGIS for Desktop 10.x or the Python 3.6 interpreter for ArcGIS Pro.

Options for using an existing interpreter in PyCharm.

PyCharm can be used to easily switch between Python 2.7 and Python 3.6, as well as between multiple environments for ArcGIS Pro—all with a single installation of PyCharm. This ability makes PyCharm a robust IDE for complex projects with multiple versions or environments.