Exercise 2
Working with Python editors
Exercise data
Exercise data for this book can be downloaded from [Link]
This is a link to the ArcGIS Online group called Python Scripting for ArcGIS Pro (Esri Press). The
data for Exercise 2 is posted as a zip file called PythonScripting_Ex02_Data.zip. Download this
file and extract it to a folder of your choice. The instructions use a folder called C:\PythonPro,
but you can use a different folder provided you update any paths.
Python editors
This exercise shows you how to install and configure three different Python editors:
IDLE, Spyder and PyCharm. The remaining exercises for this book mostly use IDLE but Spyder
and PyCharm are used for selected tasks. Most of the exercises can also be completed using a
different editor, provided it is properly configured to use the default environment arcgispro-py3
or a clone.
This exercise also reviews the use of the Python window to write and test short snippets
of code. The Python window is used in some of the remaining exercises, but it does not provide
all the functionality of a regular Python editor.
Using the built-in shortcut to use IDLE
IDLE is a basic Python editor that is part of any Python installation. To work with ArcPy,
IDLE must be configured to use the default environment arcgispro-py3 or a clone. This
environment ensures the correct version of Python is used and that ArcPy is licensed.
The installation of ArcGIS Pro does not create a program shortcut for IDLE, but the
following steps will show how to run IDLE and create your own desktop shortcut.
1. Start File Explorer in Windows, and navigate to the C:\PythonPro\Ex02 folder.
2. Locate the file test_script.py
Being able to see the file extensions is helpful for many tasks. If you see the file
test_script instead of test_script.py, make sure file extensions are visible.
3. Make file extensions visible.
4. Right-click on the test_script.py file, and click Edit with IDLE (ArcGIS Pro).
You may have other options showing, depending on the configuration of your computer.
For example, the option Edit with IDLE is available if you have ArcGIS Desktop 10.x installed, but
it does not use the correct version of IDLE. Use only Edit with IDLE (ArcGIS Pro) to ensure you
are using the correct Python environment.
The script file opens in IDLE for Python version 3.6.9. You can resize the script window.
5. Click Run > Run Module to run the script.
The Python Shell opens, and the result prints to the interactive window.
Opening IDLE using this built-in shortcut is convenient because it uses the active
environment of ArcGIS Pro, regardless of whether ArcGIS Pro is running or not.
Creating a desktop shortcut for IDLE
You can also create a desktop shortcut for IDLE.
1. Right-click on your desktop, and click New > Shortcut.
2. In the Create Shortcut dialog box, enter or browse to the following path:
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Scripts\[Link]
3. Click Next.
4. In the next panel of the dialog box, enter a meaningful name—for example,
IDLE for Pro.
5. Click Finish.
A new shortcut is created on your desktop with a generic icon.
You can replace this generic icon with the IDLE icon.
6. Right-click on the new shortcut, and click Properties.
7. In the Properties dialog box, click on the Shortcut tab, and then click Change
Icon.
8. In the Change Icon dialog box, browse to the following location:
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\idlelib\Icons\[Link]
9. Click OK to close the Change Icon dialog box.
10. Click OK to close the Properties dialog box.
The icon is updated for the shortcut.
Keep in mind that this shortcut is only for a specific environment. If you have created a
different environment, you must create a new shortcut to use this environment.
Running code using IDLE
You can now use the shortcut to start IDLE and practice running simple code.
1. Double-click the IDLE for Pro desktop shortcut.
The Python Shell for Python 3.6.9 opens.
The Python Shell is also referred to as the interactive window or the interactive
interpreter of IDLE.
2. At the prompt, enter the following code:
>>> print("Hello World")
There is no need to type the characters >>>. They are shown here to make it clear that
you should enter the code in the interactive interpreter at the prompt.
3. Press Enter at the end of the first line of code.
The line of code is executed, and the result is printed to the next line. A new command
prompt appears below the result.
Next, you will run the same code by using a script file.
4. Click File > New File.
A new script window opens. You can resize the script window.
5. In the script window, enter the following code:
print("Hello World")
6. Click File > Save As.
7. In the Save As dialog box, navigate to the C:\PythonPro\Ex02 folder, and save
the file as hello_idle.py. There is no need to type the .py file extension if the file
type is set to Python files.
8. Click Run > Run Module.
The interactive interpreter opens, and a confirmation appears that the script
C:\PythonPro\Ex02\[Link] was run. The results of running the script prints below, followed by
a new command prompt.
9. Close all open IDLE windows.
Note: In these exercises, when the instructions use the >>> symbol, the code should be run in
the interactive interpreter. When the instructions do not use the >>> symbol, the code should
be run as a stand-alone script.
Creating a cloned environment to install Spyder
Another commonly used Python editor is Spyder. Spyder is available as a Python
package but this package is not part of the default environment. Because the default
environment cannot be modified, installing Spyder means you first must clone the default
environment.
1. Start ArcGIS Pro. You can open any of your existing projects or start with a
blank template—for these steps, it makes no difference.
2. On the Project tab, click Python.
3. Confirm the active environment.
If you have not previously created a different environment, the default environment
arcgispro-py3 is shown.
If a different environment is shown, you can choose to install the Spyder package as part
of that cloned environment, or you can choose to first create a different clone.
4. Click Manage Environments.
5. In the Manage Environments dialog box, click on the Clone icon next to the
default environment. In the Clone Environment dialog box, confirm the name
and location of the cloned environment.
The defaults are typically OK here, unless you have many different environments.
6. Click Clone to start the cloning process.
Once the cloning process is complete, the new environment is listed in the Manage
Environments dialog box.
7. Select the cloned environment as the active environment, and click OK.
8. Restart ArcGIS Pro for the environment changes to take effect.
9. On the Project tab, click Python to confirm the active environment.
Next, you can install Spyder as a package.
Installing Spyder
In the Python Package manager, you can install Spyder as a package by adding it to the
cloned environment.
1. Click Add Packages, and scroll through the list of packages until you see the
name spyder.
2. Select the spyder package, and click Install.
Note: If the Install button is dimmed, you are still using the default environment. Make sure to
do these steps using a clone as the active environment.
3. In the Install Package dialog box, check the box to agree to the terms and
conditions, and click Install.
Once the package is installed, you can launch Spyder by typing spyder in the Python
command prompt, but is it more convenient to create a desktop shortcut.
4. Close ArcGIS Pro.
5. Right-click on your desktop, and click New > Shortcut.
6. In the Create Shortcut dialog box, enter or browse to the following path:
C:\Users\<Your Name>\AppData\Local\ESRI\conda\envs\arcgispro-py3-
clone\Scripts\[Link]
If you installed the cloned environment in a different location, the path will be slightly
different.
7. Click Next.
8. In the next panel of the dialog box, enter a meaningful name—for example,
Spyder.
9. Click Finish.
A new shortcut is created on your desktop with a generic icon.
You can replace this generic icon with the Spyder icon.
10. Right-click on the new shortcut, and click Properties.
11. In the Properties dialog box, click on the Shortcut tab, and then click Change
Icon.
12. In the Change Icon dialog box, browse to the same location as the [Link]
file used earlier, but select the file [Link].
13. Click OK to close the Change Icon dialog box.
14. Click OK to close the Properties dialog box.
The icon for the shortcut is updated.
Keep in mind that this shortcut is for only one specific environment. If you create
another clone environment and want to use Spyder to work with this environment, you must
create a new shortcut.
Running code in Spyder
Next, you will run some code in Spyder.
1. Double-click the Spyder desktop shortcut.
The Spyder application opens with a temporary script file and several other panels. The
location of the temporary script file will vary. The IPython console is like the interactive
interpreter in IDLE where you can execute code one line at a time.
Note: The default display theme of Spyder varies with the version. The preceding figure shows a
dark theme, but you can customize it by clicking Tools > Preferences > Appearance. Typically, a
lighter theme makes the syntax highlighting a bit easier to see, but the display theme is mostly
a matter of preference. The remaining instructions use a light theme.
2. At the prompt in the IPython console, enter the following code:
In [1]: print("Hello World")
There is no need to type the characters In [1]. They are shown here to make it clear
that you should enter the code at the prompt in the console.
3. At the end of the first line of code, press Enter.
The line of code is executed, and the result is printed to the next line. A new input
prompt appears below the result.
Next, you will run the same code by using a script file.
4. Select the text in the [Link] script file and delete it.
5. Enter the following code in the script:
print("Hello World")
6. Click File > Save As.
7. In the Save As dialog box, navigate to the C:\PythonPro\Ex02 folder, and save
the file as hello_spyder.py.
8. In the toolbar on the top right, click the File Explorer button. Navigate to
C:\PythonPro\Ex02, and view the scripts in this folder.
The folder contents confirm where the hello_spyder.py script is saved, as well as any
other scripts and files in the same folder.
Next, you are ready to run the script
9. Click the Run icon .
The script executes, and a confirmation appears in the console that the script
C:\PythonPro\Ex02\hello_spyder.py was run. The results of running the script print below,
followed by a new input prompt.
10. Click File > Quit to close Spyder.
Spyder provides additional functionality compared with IDLE, but writing and running
code follows a similar process, and the actual code is identical.
Installing and configuring PyCharm
PyCharm is another popular Python editor, with a lot of extra features. It is not a Python
package, and it must be downloaded and installed separately from ArcGIS Pro.
1. Open a web browser, and navigate to [Link]
2. Click Download.
3. Click on the download option for the Windows Community edition, which is
free and open source.
4. Follow the on-screen instructions to download and install PyCharm.
Under Installation Options, you can select the option to create a desktop shortcut
(recommended) and create a file association with .py files (recommended only if you plan to
make PyCharm your primary editor).
Note: The latest version of PyCharm at the time of writing is 2020.1, but this will change over
time as new updates are released. You always should use the latest version of PyCharm even
though the version is different from the one referenced in these instructions or the book.
Installation instructions can be found at [Link]
[Link]. The Standalone installation is recommended.
Once installation is complete, a new desktop shortcut is added.
Configuring PyCharm
Following installation, PyCharm must be configured to use the correct environment to
work with ArcGIS Pro.
1. Double-click the PyCharm desktop icon.
2. If prompted to import settings, select the option "Do not import settings" and
click OK.
3. You are prompted to set your display theme. The options are Darcula (light
text on dark background) and Light (dark text on white background). Select
your preference.
The instructions in this exercise use the Light theme, but either option is fine.
For the remaining setup steps, the default settings will suffice. Once you become more
familiar with the functionality of PyCharm, all these settings can be changed.
4. Click Skip Remaining and Set Defaults.
Once the initial setup of PyCharm is complete, you are prompted to create a new
project or open an existing one.
5. Click Create New Project.
The New Project dialog box opens. Here is where you set the location of your project
and the Python environment (referred to as the project interpreter in PyCharm).
6. For the location, navigate to the C:\PythonPro\Ex02 folder.
7. For the project interpreter, select the Existing interpreter option.
PyCharm does not automatically recognize the environments created by ArcGIS Pro, so
you must select them manually.
8. For the existing interpreter, click on the ellipsis (…) icon on the right of the
entry box.
9. In the Add Python Interpreter dialog box, click Conda Environment.
10. For the interpreter, click on the … icon on the right of the entry box.
11. Navigate to the following location:
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\[Link]
12. Check the option to make this environment available to all projects.
13. Click OK.
The default environment arcgispro-py3 now shows up as the interpreter in the New
Project dialog box.
14. Click Create.
15. If you are prompted that the directory is not empty, click Yes to create a
project from existing sources.
PyCharm opens with a project tab on the left side showing the project folder and any
scripts located inside this folder.
The initial configuration steps are a bit cumbersome, but it is essential that you use the
correct environment in PyCharm. Contrary to IDLE and Spyder, which are configured
automatically to use a single environment, PyCharm can use many different environments. This
is one of its strengths, but it also implies that you may inadvertently use an incorrect
environment. Therefore, when using PyCharm to write Python scripts for ArcGIS Pro, always
make sure you are using the arcgispro-py3 environment or a clone.
Future projects will automatically use the selected environment, but you can configure
PyCharm to use newly created environments as well.
16. Click File > New Project.
The Create Project dialog box appears, and the arcgispro-py3 environment is selected by
default as the interpreter.
17. Close the Create Project dialog box to return to the current project.
You can also manage the interpreters used by PyCharm by clicking File > New Project
Settings > Settings for New Projects. In the Settings for New Projects dialog box, click Project
Interpreter in the left panel. The drop-down option lists all available interpreters.
Click on the cog icon on the right side, then click Add to bring up the Add Python
Interpreter dialog box. Here, you can point to a new environment—for example, after creating
a clone using the Python Package Manager.
A single installation of PyCharm can employ any number of environments. Contrary to
IDLE and Spyder, you need to install PyCharm only once, and then you can modify the
environment within the application.
18. Click Cancel twice to return to the current project in PyCharm.
The remainder of the exercises that accompany the Python Scripting for ArcGIS Pro book
use only the default environment, or a cloned environment if you use Spyder as your Python
editor. There is no need to switch environments on a regular basis for the exercises.
Running code in PyCharm
Next, you will run some code in PyCharm
1. Return to the main interface of PyCharm.
The Project tab on the left shows the folder where the project resides, including any
Python files that have previously been created. Now you need a place to write your code.
2. On the top menu, click View > Tool Windows > Python Console.
This brings up a Python console, like the IPython console in Spyder and the interactive
interpreter in IDLE.
3. At the prompt in the Python console, enter the following code and press Enter:
print("Hello World")
The line of code is executed, and the result is printed to the next line. A new input
prompt appears below the result. This functionality is identical to IDLE and Spyder, although the
display is slightly different.
Next, you will run the same code by using a script file.
4. Right-click on the C:\PythonPro\Ex02 folder in the Project panel, and click
New > Python file.
5. In the New Python file dialog box, type hello_pycharm.
6. Press Enter to create the new script file.
The file is added to the project folder and opens in a script window.
7. Enter the following code in the script:
print("Hello World")
The script file is saved automatically as you write the code.
8. Right-click inside the script file, and click Run 'hello_pycharm'. Alternatively,
on the top menu, click Run > Run 'hello_pycharm.py'.
A new Run window opens with the results from running the script. This window is
separate from the Python console.
9. On the top menu, click File > Exit to close PyCharm.
Note: For the remainder of the exercises that accompany the Python Scripting for ArcGIS Pro
book, whenever you are asked to write a script, you are expected to create a new script (.py
file) in a Python editor. You can use IDLE, Spyder, PyCharm, or another editor of your choice.
You are encouraged to try several editors.
Running code in the Python window
An alternative to running Python code in a stand-alone Python editor is to use the
Python window in ArcGIS Pro. The Python window is a convenient way to practice writing
Python code and test short snippets of code. Longer and more complex scripts are best written
in a stand-alone Python editor with more functionality.
1. Start ArcGIS Pro with a new empty project.
2. On the Analysis tab, click on Python.
The Python window opens. Since you are running the Python window from within
ArcGIS Pro, the active environment in the ArcGIS Pro application is being used here, and no
additional configuration is required. This is one of the benefits of using the Python window.
3. In the prompt section of the Python window, enter the following code, and
press Enter:
print("Hello World")
The line of code is copied to the transcript section of the Python window, and the result
appears on the next line in the transcript.
The Python window allows for running individual lines of code, but there is no separate
script window. The Python window is therefore comparable to the interactive interpreter in
IDLE or the console in Spyder and PyCharm.
One of the benefits of working in the Python window is that you can interact with the
ArcGIS Pro application.
4. Make sure that the Catalog pane is visible. If it is not visible, on the View tab,
click Catalog Pane.
5. In the Catalog pane, right-click Folders, and add a folder connection to
C:\PythonPro\Ex02.
6. Expand the Ex02 folder, right-click on the shapefile [Link], and click Add
To New > Map.
7. In the prompt section of the Python window, enter the following code, and
press Enter:
print(arcpy.GetCount_management("parks"))
Running the code prints the count of the number of features. Note that you can
reference the name of the layer (i.e., parks) without having to specify the location or file
extension.
Accomplishing the same task in a stand-alone Python editor requires specifying the full
path and file name of the feature class—i.e., C:\PythonPro\Ex02\[Link].
Note: The syntax of working with geoprocessing tools is covered in depth in exercise 5.
End of exercise 2.