ArcGIS Pro 2.8 comes with a new handy feature that copies the Python command when running a geoprocessing tool inside Pro. This tutorial shows how the tool works, describes some best use cases and how the interpret the results.
Pro 2.8 introduces a new handy feature that copies the corresponding Python command when running a geoprocessing tool. What is interesting about this new Copy Python Command is that it copies all tool parameters that are set by the user, as well as the specified file paths and the geoprocessing tool name and corresponding Python submodule. What’s even better, is that you don’t have to run the tool to be able to copy the Python snippet that includes both the specified parameters and environments.
This new tool has multiple benefits for the user:
- You can use it to create a Python script quickly by copy-pasting multiple code snippets from various tools that are required for a certain workflow.
- You don’t have to consult the online documentation of ArcGIS Pro to look for the name or syntax of a geoprocessing tool as it is automatically created for you by the Copy Python Command.
- The Copy Python Command automatically translates all necessary environments and parameters to Python code, saving you time and effort.
- By pasting the Python snippet into a code or text editor, you can adjust it to your needs quickly before actually running it.
Let’s have a look at where the tool is found and how it works, using a single geoprocessing tool as an example. To follow along, download the Natural Earth QuickStart kit and open a new, empty project in Pro.
Using the Copy Python Command with the Get Count geoprocessing tool
For this example, we’ll use the ne_110m_populated_places.shp file that is found in the 110m_cultural subfolder of the Natural Earth dataset. It shows several cities on earth, displayed as point features:
Click the Copy Python Command option and open an empty notebook inside Pro or text editor of your choice, then right-click the new, empty file and choose “paste”. (Notice that the Get Count tool is not run until you actually click the “Run” button.) After clicking “paste”, you’ll see the following Python snippet appearing on your screen:
arcpy.management.GetCount(“ne_110m_populated_places”)
This snippet starts with referencing the arcpy site package, then the data submodule inside it (here simply renamed “management”), then the GetCount method inside it, with the named parameter “ne_110_populated_places”, which refers to the shapefile of the same name. A few things are important to notice:
- The snippet produced by the Copy Python Command is not a full Python script, only a code snippet containing the tool name, parameters and environments. Running this code inside an ArcGIS Notebook will cause errors: what’s missing is the “import arcpy” statement preceding this line of code so that Pro can access its functionality.
- The Python code snippet from above does not show a relative or absolute file path between the parentheses, but a named reference to the shapefile using commas. To have this snippet work in a standalone Python script, you would need to manually edit the snippet and place a relative or absolute path between the parentheses, otherwise Python does not know where to find the input shapefile on disk. When using other geoprocessing tools, I noticed the Copy Python Command sometimes does use relative paths, so it which type of file reference is used depends on the geoprocessing tool that is used.
Summary
In this short tutorial, we’ve demonstrated how the Copy Python Command inside ArcGIS Pro 2.8 works. This tool translates geoprocessing environments and parameters to Python code snippets that can be copied and edited inside code or text editors. What is very nice is that you can produce these Python snippets without running a geoprocessing tool, which will save a lot of time and effort from the part of the user. Hopefully, this functionality will be extended in future versions of Pro for reproducing charts and graphs as well.