Generate Python and ArcPy code with the ArcGIS Pro assistant (beta)
Use the ArcPy Code mode of the ArcGIS Pro assistant (beta) to generate Python code that uses the ArcPy site package.
From a natural-language prompt, the assistant generates Python code that can be used in ArcGIS Pro Notebooks, the Python window, script tools, external Python editors, and other Python environments.
The assistant can generate code for simple tasks, multistep automation workflows, project-aware scripting scenarios, and script tool development. It can use available information about maps, layers, and tables in ArcGIS Pro to generate context-aware code.
Note:
The assistant does not run generated code automatically. Generated code is displayed for review and runs only when you choose to run it.
Generate code
To generate Python code with the assistant, complete the following steps:
In the Assistant (Beta) pane, click the ArcPy Code card, or click the menu button
and click ArcPy Code
.In the prompt text box, enter a natural-language description of the task you want to perform.
Press Enter or click Ask the question
to submit the prompt.As it processes the request, the assistant typically outlines its planned approach and indicates the actions it takes to evaluate project content, tools, and ArcPy functions. When it finishes, it provides a brief description of what the code does, followed by the generated Python code.
Review and use the generated code
To use the generated code, complete the following steps:
Review the entire code block carefully before running it to confirm that it performs the intended task and identify any changes it makes to your data.
Important:
The code blocks generated by the assistant may require updates to dataset paths, variable names, parameter settings, credentials, and other environment-specific values before use. See the ArcGIS Pro Python reference and ArcGIS Pro geoprocessing tool reference for additional information.
Optionally, ask follow-up questions to refine the generated code, request additional explanation, or modify the workflow that the code implements.
Optionally, use the reviewed code in one of the following ways:
In the response, below the code block window, click Run
to run the code from the Assistant (Beta) pane using the ArcGIS Pro embedded Python environment.A prompt requesting that the assistant run the validated code is submitted. The assistant validates the code before running it and may use current project context to resolve referenced content. If validation succeeds, it runs the code.
Note:
Validation does not guarantee that the code will run successfully with every dataset, path, license, or runtime configuration.
At the top of the code block window, click the Send to drop-down arrow, and click Send to Python window to insert the code into the Python window prompt. From there, you can run the code by pressing Enter.
Click the Send to drop-down arrow, and click Send to Notebook to insert the code into a new cell in the project's open and active ArcGIS Notebook.
At the top of the code block window, click Copy Code
to copy the code for use in another Python interface or script tool editor.
Project-aware code generation
The assistant can use available information about maps, layers, and tables in ArcGIS Pro to generate code. The assistant does not directly inspect source data values, such as attribute values or raster cell values, while generating code.
Caution:
Data that you include in a submitted prompt is sent to the assistant. When you run code from the Assistant (Beta) pane, the assistant can also receive text that the code writes to output, including print statements, geoprocessing messages, and errors. The assistant does not receive other data that the code processes.
Do not provide sensitive, confidential, or otherwise restricted data unless you are authorized to share it. If your organization has data security requirements, review the generated code for print statements before running it.
The assistant supports the following project-aware scripting capabilities:
Generate code that references information in maps and layers in the active project
Generate code that uses map layer field names
Create workflows that use the ArcPy Mapping module,
arcpy.mp, which is capable of managing projects, maps, layouts, reports, and layer objectsGenerate different processing logic for feature layers and raster layers
Produce automation workflows that adapt to the current project structure instead of relying on hard-coded resource names
The assistant cannot do the following:
Directly inspect data values while generating code
Directly read the files or folders on your computer
Directly access websites that you specify
Generate script tool code
The assistant can generate code for use in ArcGIS Pro script tools. This code can use the ArcPy GetParameter function to retrieve input parameter values and the SetParameter function to set output parameter values rather than relying on hard-coded paths and project resources.
After generating code, you must still configure the script tool parameters using the Tool Properties dialog box.
Learn more about setting script tool parameters
Known limitations (beta)
The assistant has the following limitations:
The assistant does not directly access, modify, or create files and folders on your computer. Generated ArcPy code, as with any Python script, can access, modify, or create files and folders on your computer when run. File paths used in generated code originate from information you provide or from available project resources.
The assistant may produce inaccurate, incomplete, or nonfunctional code.
Important:
Carefully review all generated code before running it. The assistant generates suggestions and examples but does not validate that generated code is appropriate for every workflow or dataset.