Python in ArcGIS Pro

Python is a free, cross-platform, open-source programming language. It is widely used and supported. To learn more about Python, visit the Python website.

Python was introduced to the ArcGIS community with ArcGIS 9.0. Since then, it has been accepted as the scripting language of choice for geoprocessing users and continues to grow.

Learning Python

In addition to the ArcGIS Pro Python help, you can learn more from the Learn Python in ArcGIS Pro ArcGIS Learn lessons and the following Esri Press books by Paul A. Zandbergen, regardless of your skill level or experience:

A suitable Python reference book is recommended to augment the ArcPy-focused information included here. There are many books on Python and its uses, with new ones released regularly, so explore what is available. The Python website has full documentation for Python, but it is concise and developer oriented. There is a large online Python community with many online resources that are accessible from the Python home page.

Python tutorials

If you are new to Python, the external tutorials listed here are recommended:

Python tutorials for programmers

The external tutorials listed here are for those who have experience with other programming languages:

Conda

Python is the primary language for automation in ArcGIS Pro, due in large part to its versatility and extensibility. It is partially due to these qualities that Python is also becoming one of the most widely used programming languages in general. Python has a variety of preexisting packages that can be used in ArcGIS. However, managing which packages are installed on a system can be a complex and time-consuming task, especially when working on multiple projects or trying to share code with others.

To leverage this versatility, the Python community has created methods to create projects in multiple versions of Python and simplify the process of installing nearly all publicly available Python packages. Conda is a popular and widely used Python package manager.

The functionality of conda is integrated into ArcGIS Pro through the Package Manager. The Package Manager removes many of the challenges faced when writing Python code. It supports installing open-source and third-party libraries that are associated with an individual project, rather than the base Python installation. This simplifies the process of successfully sharing complex Python tools across multiple computers.

Run Python

You can run Python interactively in ArcGIS Pro using the Python window or ArcGIS Notebooks. The Python window provides a prompt from which short snippets of Python code can be run. ArcGIS Notebooks are based on the Jupyter Notebook architecture and offer an experience in which code, visualizations, and narrative text can be run and stored together in one document.

You can run Python scripts (files with a .py extension) outside of ArcGIS Pro. The subsections below describe how to run Python scripts from outside the application with the correct ArcGIS Pro Python 3 conda environment activated.

Start from a command prompt

To start Python from a command prompt, type the following:

"%PROGRAMFILES%\ArcGIS\Pro\bin\Python\Scripts\propy"
Nota:

All paths shown assume a per-machine (all users) installation of ArcGIS Pro. If ArcGIS Pro has been installed for the current user, substitute %LOCALAPPDATA% for %PROGRAMFILES%.

To run a Python script from a command prompt (arguments can be added following the .py extension), type the following:

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

Start the ArcGIS Pro Python 3 conda environment:

"%PROGRAMFILES%\ArcGIS\Pro\bin\Python\Scripts\proenv"

Start from a batch file

To run a Python script from a .bat file, use call on propy:

@echo Run my Python script
call "%PROGRAMFILES%\ArcGIS\Pro\bin\Python\Scripts\propy" myscript.py
@echo Finished

Run as a scheduled task

If you are running a Python script as a scheduled task, the program or script should be C:\PROGRA~1\ArcGIS\Pro\bin\Python\Scripts\propy, and the arguments should include the full path to the Python script.

Shortcuts

Use the following shortcuts to access Python:

ShortcutDescription

Python Command Prompt

Opens a command prompt window initialized with the current conda environment. To start an interactive Python session, type python. To run a Python script, type python followed by the path of a .py file.

Python Interactive Terminal

Opens a Python session with the current conda environment.

Python IDEs

Most modern Python integrated development environments (IDE) can be used seamlessly with conda by configuring the version of Python that the IDE uses. The primary step involves directing the IDE to use the correct Python.exe file in the ArcGIS Pro installation. To configure several common IDEs, see Using IDEs in the conda documentation or the documentation for the particular IDE.

You can launch Python IDLE from a conda environment by typing idle.

Migration

If you are migrating Python code written to a version of ArcGIS Desktop (or ArcGIS Server or ArcGIS Engine), see Python migration from 10.x to ArcGIS Pro. The primary differences are that ArcGIS Pro uses Python 3 and other ArcGIS products use Python 2, and ArcPy has some differences in the tools it includes. For example, the arcpy.mapping module is replaced by the arcpy.mp module.

Troubleshooting

ArcPy must be run from inside a conda environment, which ArcGIS Pro uses to manage the installation of Python. To access this environment, open Python from the links in the start menu group or run proenv in a terminal.

If a conda environment is opened in a command window without the necessary administration privileges, a message appears indicating the lack of permissions. Close the command window, reopen it, and run as an administrator.

Authorize Python outside the application

If you run Python scripts that use ArcGIS Pro functionality outside of the ArcGIS Pro application, such as a Python IDE, from a command prompt, or running scripts through scheduled tasks, one of the following conditions must be met:

  • Sign me in automatically is checked when signing in to ArcGIS Pro.
  • ArcGIS Pro is currently open.
  • ArcGIS Pro has been authorized to work offline.
  • ArcGIS Pro is configured with a Concurrent Use license and at least one license is available on the ArcGIS License Manager.
Nota:

If one of the above conditions is not met, a RuntimeError: NotInitialized exception appears when importing arcpy or arcgisscripting.

Learn more about licensing ArcGIS Pro

Temas relacionados