Python in ArcGIS Pro

Python is a free, cross-platform, open-source programming language that is both powerful and easy to learn. It is widely used and supported. To learn more about Python, visit python.org.

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. Each release has furthered the Python experience, providing more capabilities and a richer Python-friendly experience.

Esri has fully embraced Python for ArcGIS and sees Python as the language that fulfills the needs of the Esri user community. Here are some of the advantages of Python:

  • Easy to learn and suitable for both beginners and experts
  • Highly scalable, suitable for large projects or small one-off programs known as scripts
  • Portable, cross-platform
  • Embeddable (making ArcGIS scriptable)
  • Stable and mature
  • A large user community

Migration

If you are migrating Python code written to a version of ArcGIS Desktop (or ArcGIS Server or ArcGIS Engine), see Python migration for 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.

Learning Python

In addition to the ArcGIS Pro Python help, the following Esri Press books by Paul A. Zandbergen will be invaluable, regardless of your skill level or experience:

A suitable Python reference book is recommended to augment the ArcPy-focused information included here. For Python beginners, Learning Python by Mark Lutz and David Ascher, published by O’Reilly & Associates, and Core Python Programming by Wesley J. Chun, published by Prentice Hall, are both good introductions to the language and are not overwhelming in scope. There are many other books on Python and its uses, with new ones being 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 (Perl, Visual Basic, C):

Conda

Python is the primary language for automation in both ArcGIS 10.x and ArcGIS Pro, due in large part to its versatility and extensibility. It is partially due to these virtues that Python is also becoming one of the most widely used programming languages in general. Python has a rich ecosystem of preexisting packages that can be leveraged in ArcGIS but 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 the most popular and widely used Python package manager.

The functionality of conda is integrated into ArcGIS Pro through the Python Package Manager. The Python Package Manager removes many of the hurdles 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.

Learn more about conda in ArcGIS Pro

Run Python

Python can be run 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 executed. ArcGIS Notebooks are based on the Jupyter Notebook architecture and offer an experience in which code, visualizations, and narrative text can be executed and stored together in one document.

Python scripts (files with a .py extension) can be run 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.

From a command prompt

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

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

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 file), 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"

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

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 your Python script.

Shortcuts

The following shortcuts can be used 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 or the documentation for the particular IDE.

Python IDLE can be launched from a conda environment by typing idle.

Troubleshooting

ArcPy must be run from inside the conda environment, which ArcGIS Pro uses to manage the installation of Python. You can access this environment by launching Python from the links in the ArcGIS > ArcGIS Pro start menu group or by running 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 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.
Note:

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.

Related topics