Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Programming ArcGIS with Python Cookbook, Second Edition

You're reading from   Programming ArcGIS with Python Cookbook, Second Edition Over 85 hands-on recipes to teach you how to automate your ArcGIS for Desktop geoprocessing tasks using Python

Arrow left icon
Product type Paperback
Published in Jul 2015
Publisher
ISBN-13 9781785282898
Length 366 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Eric Pimpler Eric Pimpler
Author Profile Icon Eric Pimpler
Eric Pimpler
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Fundamentals of the Python Language for ArcGIS 2. Managing Map Documents and Layers FREE CHAPTER 3. Finding and Fixing Broken Data Links 4. Automating Map Production and Printing 5. Executing Geoprocessing Tools from Scripts 6. Creating Custom Geoprocessing Tools 7. Querying and Selecting Data 8. Using the ArcPy Data Access Module with Feature Classes and Tables 9. Listing and Describing GIS Data 10. Customizing the ArcGIS Interface with Add-ins 11. Error Handling and Troubleshooting 12. Using Python for Advanced ArcGIS 13. Using Python with ArcGIS Pro A. Automating Python Scripts B. Five Python Recipes Every GIS Programmer Should Know Index

Using IDLE for Python script development

As mentioned in the preface, when you install ArcGIS for Desktop, Python is also installed along with a tool called IDLE that allows you to write your own code. IDLE stands for Integrated DeveLopment Environment. Since it is available with every ArcGIS for Desktop installation, we'll use IDLE for many of the scripts that we write in this book along with the Python window embedded in ArcGIS for Desktop. As you progress as a programmer, you may find other development tools that you prefer over IDLE. There are many other development environments that you may want to consider, including PyScripter, Wingware, Komodo, and others. The development environment you choose is really a matter of preference. You can write your code in any of these tools.

The Python shell window

To start the IDLE development environment for Python, you can navigate to Start | All Programs | ArcGIS | Python 2.7 | IDLE. Please note that the version of Python installed with ArcGIS will differ depending upon the ArcGIS version that you have installed. For example, ArcGIS 10.3 uses Python 2.7, whereas ArcGIS 10.0 uses version 2.6 of Python.

A Python shell window similar to this screenshot will be displayed:

The Python shell window

The Python shell window is used for output and error messages generated by scripts. A common mistake for beginners is to assume that the geoprocessing scripts will be written in this shell window. This is not the case. You will need to create a separate code window to hold your scripts.

Although the shell window isn't used to write entire scripts, it can be used to interactively write code and get immediate feedback. ArcGIS has a built-in Python shell window that you can use in a similar way. We'll examine the ArcGIS Python window in the next chapter.

The Python script window

Your scripts will be written in IDLE inside a separate window known as the Python script window. To create a new code window, navigate to File | New Window from the IDLE shell window. A window similar to this will be displayed:

The Python script window

Your Python scripts will be written inside this new code window. Each script will need to be saved to a local or network drive. By default, scripts are saved with a .py file extension to signify that it is a Python script.

Editing existing Python scripts

Existing Python script files can be opened by selecting File | Open from the IDLE shell window. Additionally, a Python script can be opened from Windows Explorer by right-clicking on the file and selecting Edit with IDLE, which brings up a new shell window along with the script loaded in the Python script editor. You can see an example of this in the following screenshot:

Editing existing Python scripts

In this instance, we have loaded the ListFeatureClasses.py script with IDLE. The code is loaded inside the script window:

Editing existing Python scripts

Now that the code window is open, you can begin writing or editing code. You can also perform some basic script debugging with the IDLE interface. Debugging is the process of identifying and fixing errors in your code.

Executing scripts from IDLE

Once you've written a geoprocessing script in the IDLE code window or opened an existing script, you can execute the code from the interface. IDLE does provide functionality that allows you to check the syntax of your code before running the script. In the code window, navigate to Run | Check Module to perform a syntax check of your code.

Any syntax errors will be displayed in the shell window. If there aren't any syntax errors, you should just see the prompt in the shell window. While the IDLE interface can be used to check for syntax errors, it doesn't provide a way to check for logical errors in your code nor does it provide more advanced debugging tools found in other development environments, such as PyScripter or Wingware.

Once you're satisfied that no syntax errors exist in your code, you can run the script. Navigate to Run | Run Module to execute the script:

Executing scripts from IDLE

Any error messages will be written to the shell window along with the output from the print statements and system-generated messages. The print statement simply outputs text to the shell window. It is often used to update the status of a running script or to debug the code.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime