Book Image

Programming ArcGIS with Python Cookbook, Second Edition

By : Donald Eric Pimpler, Eric Pimpler
Book Image

Programming ArcGIS with Python Cookbook, Second Edition

By: Donald Eric Pimpler, Eric Pimpler

Overview of this book

Table of Contents (22 chapters)
Programming ArcGIS with Python Cookbook Second Edition
About the Author
About the Reviewers

Using the ArcGIS Python window

In this recipe, you'll learn how to use the ArcGIS Python window. In the last section, you learned how to use the IDLE development environment for Python, so this section will give you an alternative to write your geoprocessing scripts. Either development environment can be used, but it is common for people to start writing scripts with the ArcGIS for Desktop Python window and then move on to IDLE or another development environment when scripts become more complex.

The ArcGIS Python window

The ArcGIS Python window is an embedded, interactive Python window in ArcGIS for Desktop 10.x. It is newer and ideal for testing small blocks of code, learning Python basics, building quick and easy workflows, and executing geoprocessing tools. For new programmers, the ArcGIS Python window is a great place to start!

The ArcGIS Python window has a number of capabilities in addition to being the location to write your code. You can save the content of the window to a Python script file on a disk or load an existing Python script into the window. The window can be either pinned or floating. While floating, the window can be expanded or contracted as you wish. The window can also be pinned to various parts of the ArcGIS display. You can also format the font and text colors displayed in the window by right-clicking on the window and selecting Format.

Displaying the ArcGIS Python window

The Python window can be opened by clicking on the Python window button on the Standard ArcGIS for Desktop toolbar, as seen in the screenshot. This is a floating window, so you can resize as needed and also dock it at various places on the ArcMap interface:

The Python window is essentially a shell window that allows you to type in statements one line at a time, just after the >>> line input characters. On the right-hand side of the divider, you will find a help window.

You can load an existing script by right-clicking inside the Python window and selecting Load… from the menu. You can also format the font and text colors displayed in the window by right-clicking on the window and selecting Format. You will be provided with White and Black themes; you can select fonts and colors individually:

Click on the Set Black Theme button to see an example. If you spend a lot of time writing code, you may find that darker themes are easier on your eyes:

The ArcGIS Python window also provides code-completion functionalities that make your life as a programmer much easier. You can try this functionality by opening the ArcGIS Python Window and typing arcpy followed by a dot on the first line. ArcPy is a module-oriented package, which means that you access the properties and methods of an object using a dot notation. Notice that a drop-down list of available items is provided. These are the tools, functions, classes, and extensions that are available for this particular object. All objects have their own associated items, so the list of items presented will differ depending on the object that you have currently selected:

This is an auto-filtering list, so as you begin typing the name of the tool, function, class, or extension, the list will be filtered according to what you have typed:

You can choose to have the Python window auto-complete the text for you by selecting an item from the list using your mouse or by using the arrow keys to highlight your choice, and then using the Tab key to enter the command. This autocompletion feature makes you a faster, more efficient programmer. Not only is it easy to use, but it also dramatically cuts down the number of typos in your code.