Python, or even scripting in general, can seem like a scary thing to those who have no background in computer languages. Even though it might seem difficult, there are many times where automating common workflows can be very helpful in speeding up the monotonous parts of GIS that we all have to do.
These 10 tips are some basic ways to learn Python and get you more comfortable using scripts. I myself had no background in scripting or programming before working at Esri. I have used all of these tips to gain the confidence and practice I needed to help support customers. And I still use them today!
- Understand the basics of Python’s syntax rules.
It’s always helpful to get to know the language before you use it. See these links for more information on the Python language and syntax.
- Use Esri training resources.
Sometimes there is nothing better than taking a course. Whether it’s self-paced or instructor led, it is good to see practical examples of how to use scripts in ArcGIS.
- Visit Esri Web Help, Python Site Package, and tool samples.
The online web-help offers many pages on Python, from classes and methods (in the ArcPy site package section) to samples of how a tool can be run from a script. This is a great place to start looking for how specific tools should be called up. Check out these links for a taste of Python in the web-help.
- Read the Geoprocessing Blog.
Check out the official Geoprocessing Blog for posts from the Esri Geoprocessing Development team, including tips on using Models and Python scripts. Samples are included on some of the posts as well.
- Visit the Python and Map Automation forums.
Discuss issues and problems with other ArcGIS Python users.
- Reference the Model and Script Tool Gallery.
Check out what others have made, find the custom tool of your dreams, or upload your own python script tools and documentation at the Model and Script Tool Gallery.
- Use the Python Window or an IDE (Integrated Development Environment).
Python scripts are basically text files with a .py extension. You could write your script in notepad, but why do that when you can use an interface that will give you some extra writing help.IDE’s are interfaces that help in script writing. Two common IDEs are IDLE and PythonWin. IDLE is automatically installed when you installed ArcGIS Desktop. PythonWin is available on our Desktop DVD or download to be separately installed.The Python Window, new at ArcGIS Desktop 10.0, allows you to use and test code within an open ArcGIS session. This is helpful when you want to see an end process say in ArcMap.
- Create a model and export it to Python.
This is a great place to start practicing syntax and parameter use. Build your model and then export it to python. Take a look at what is going on then modify the script to fit your needs.
- Create a Python tool for someone else.
Not sure what to write a script for? Ask someone for a workflow they want to automate then write a script. After the script is ready you can import it into a Geoprocessing tool for easy use.
- Apply error handling methods within your script
Using additional code to your script can help extract useful messages from failing codes. Tracebacks will allow you to take a look at where in the script exceptions are raised. Try-except statements can allow scripts to continue running and display messages when errors occur. GetMessages also will pull errors and warnings that the tool runs into. Check out these links for more information on these topics:
Links to all these courses and more can be found on the Esri Training site. (Click ‘Find Training’ and search for ‘Python’.)
Stephanie W. – Desktop Support Analyst