ArcGIS Pro 2.8’s New Copy Python Command

by | Aug 26, 2021

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:



We’ll now use the Get Count geoprocessing tool to count how many point features the shapefile contains and use the Copy Python Command before running the tool. Search for the Get Count tool by clicking “Analysis” and “Tools” next on the ribbon interface or “Geoprocessing” on the left of the screen if you have that option available. With the Get Count feature opened, select the point features layer that is visible in the table of contents and click the dropdown button right next to the “Run” button on the bottom right of the screen. You’ll now see four options, the latest being the Copy Python Command:

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.

Categories

Recent Posts

Eric van Rees
Eric van Rees is a freelance writer and editor. His specialty is GIS technology. He has more than eight years of proven expertise in editing, writing and interviewing as editor and editor-in-chief for the international geospatial publication GeoInformatics, as well as GIS Magazine and CAD Magazine, both published in Dutch. Currently, he writes about geospatial technology for various clients, publications and blogs.

Sign up for our weekly newsletter
to receive content like this in your email box.