5 Tips for Creating Python Scripts for ArcGIS Pro

If you are hoping to take your ArcGIS skills to the next level, learning how to use ArcPy and write Python scripts is a good start. Using Python Scripts to perform complex geoprocessing analyses is more efficient. In the event that something changes or you need to fix something, a script allows you to repeat your work very quickly. Scripts The following article presents five tips for creating Python Scripts in ArcGIS Pro.

An Introduction to Python in ArcGIS

As you become involved in more advanced ArcGIS Pro, figuring out a way to automate tasks becomes more important. Python is a free, open-source programming language that is widely used for a number of applications, including ArcGIS. Python was introduced to the ArcGIS community with version 9.0. Since that time, Python has become the scripting language of choice (as opposed to VBA or SQL). Python is embeddable, which makes ArcGIS “scriptable.”

You can run your Python scripts in the Python window. To open the Python window in ArcGIS Pro, click on the Analysis tab. Then, in the Geoprocessing group, click the drop-down menu under the Python button New Notebook, and click the Python window button – “Show Python window.” Finally, simply copy and paste your code into the Python window or right-click and “Load Code.”

Alternatively, you can run Python using ArcGIS Notebooks. ArcGIS Notebooks are based on the Jupyter Notebook architecture and allow the user to store code, visualizations, and narrative text in one document. For most of my work, I prefer to use the Python Window.

1. Use the Geoprocessing History Window to help write your script.

This trick will save you a lot of time, especially if you are a beginner. If you are unsure how to include a particular geoprocessing task in your script, simply run the tool like you normally wood. Then, copy the code from the Geoprocessing History window and paste it into your script. Modify as needed. The screenshots below show how to copy code from the Geoprocessing History window.

2. Use loops for repetitive tasks.

Loops allow you to cycle through a repetitive task quickly. A for loop allows you to cycle through a list or set of “iterators.” For loops also allow you to cycle through all of the files in a folder, for example. The following screenshots are examples of for loops that I have incorporated into some of my Pythons scripts.

3. Add comments for documentation.

An advantage of writing Python scripts is that you have clear documentation of your workflow. This can be useful for writing reports or for replicating an analysis months later. To make this aspect of Python scripting even more useful, add notes to yourself within the Python script. This documentation will help you (or someone else) pick up this analysis later. To add comments to your scripts, simply put the pound symbol (#) before any comment text.

4. Use a source code editor for Python scripting.

It is certainly possible to simply write your Python scripts in Notepad. However, a text editor that was built for Python (and possibly other programming languages) will make the process much easier. The most useful aspect of these editors, in my opinion, is the line numbering because this feature allows you to address error messages more easily. My favorite code editor is Notepad ++. It is lightweight and easy to use. Other popular Python code editors include Sublime, PyCharm, and Atom.

5. The best way to learn is to start working on something.

There are many books and courses available for learning Python. These references are certainly useful, but the best way to learn Python quickly is to start working on an actual project/ task. If you are unsure of how to do something, Google it or ask a question on GIS Stack Exchange. It is amazing how quickly you learn when you immerse yourself in a project.

Leave a Comment