Best Practices for ArcPy™

These best practices assume the use of ArcGIS® Pro and Python 3 

ArcGIS Pro offers a wide range of tools to support your GIS work. However, many workflows are unique to an organization. Scripting is a great way to automate organization-specific processes, saving time. With ArcPy, Esri’s Python site package, you can create your own custom GIS scripts.  

There are three types of Esri Python tools that use ArcPy: ArcGIS Pro toolboxes, standalone scripts, and geoprocessing (GP) services. 

ArcGIS Pro Toolboxes  

ArcGIS Pro toolboxes allow you to create custom tools with the same user experience as existing tools within ArcGIS Pro. This UX/UI uniformity makes the use of custom tools easier and more intuitive. ArcPy is created to run in ArcGIS Pro, so there is a high likelihood that all ArcPy libraries will work within a toolbox. ArcGIS Pro toolboxes are also easy to adapt for each run because the user can adjust the input parameters. 

How to add an ArcPy script to an ArcGIS Pro toolbox 

 

To add a script to a toolbox, right-click the toolbox and select New -> Script (You can also use a Python toolbox instead, which is a slightly different format. For this tip, we are discussing the default toolbox option).

  • Add a name, label, and description in the General tab.  

  • Define the input parameters in the Parameters tab. These can be retrieved in the script using arcpy.GetParameter({number}).  

  • The Validation tab can be used to validate inputs, such as checking the correct geometry type for a feature layer or filling out a dropdown with values from a selected feature layer. 

You can output information to the user by using arcpy.AddMessage or arcpy.AddError. Those will output to the Messages tab in the popup that appears when you click “View Details” for a running tool.  

Once your script is complete, right-click on the script, and go to Edit Metadata to add information about each input parameter for a better user experience. Users can hover over the info buttons next to each input parameter to see the metadata descriptions. 

Standalone Scripts 

Standalone scripts are useful for ETL processes, database cleanup and re-indexing, user and group management, calculations that might slow a server down during the day, daily notifications of changes, and quality control of data.  

Stand-alone scripts are run from the command line using propy and are best for automated processes. The default location of propy is: 

C:\Program Files\ArcGIS\Pro\bin\Python\Scripts\propy.  

There are important differences to note between ArcGIS Pro toolbox scripts and standalone scripts. For standalone scripts:  

Parameters should be in a config file or passed as text in the command line like normal Python scripts.   

arcpy.AddMessage() does not work, you must use the default print() call in Python.  

Some methods in ArcPy may not work as expected since the standalone scripts do not have full access to map documents and cannot display data like you can with an ArcGIS Pro toolbox. There are usually ways around this, especially if you have a blank project with a map the script can point to.  

Locating Errors 

Errors in Python can be difficult to track down, so we recommend adding a try/catch around the entire script and using the traceback library to catch any errors. You can do this for both toolboxes and stand-alone scripts. For example:  

import traceback  

try:  

… rest of the script …  

except Exception as e:  

print(str(e))  

print(traceback.format_exc())  

If you’re writing to a log file, you can also save the string output from the traceback there. 

Geoprocessing Services 

Data conversion, data analysis, and data management tools are enabled via geoprocessing services. A geoprocessing service is developed as an ArcGIS Pro toolbox and then published after a successful run in ArcGIS Pro.   

A geoprocessing service is necessary when users outside of ArcGIS Pro need access to a process, or if the tool is required as a published service for an application. For example, a custom analysis process could be published so a web application can access it and run calculations for display based on user input. 

There are ArcPy methods that may not work once published as a geoprocessing service, so it's important to test carefully once you begin publishing. The methods that may cause issues are usually related to map functions. There are usually workarounds, it may just take extra research to find the method that works for a published service.  

Note that any methods that use credits to run will also use credits once published. If you want to limit who can access your geoprocessing service, you can secure it by users or groups just like with any other published content. 

Too busy to sort out the finer details of ArcPy? Argis Solutions’ experienced GIS development team is poised to support your custom tool needs with our practical, sustainable approach to tool-building. Schedule a discovery call to learn more.

Previous
Previous

Our Work is Featured in the Spring 2024 Issue of ArcNews

Next
Next

A Look Back at 2023