Skip To Content

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 you with more capabilities and a richer, more Python-friendly experience.

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

  • Easy to learn and excellent for beginners, yet superb for 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 migrating Python code written against 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 uses Python 2, and ArcPy itself has some differences in the tools it includes and the arcpy.mapping module is replaced in favor of the arcpy.mpmodule.

If migrating from an earlier version of ArcGIS Pro (1.0 through 1.2), ArcPy itself is unchanged (except for new functionality). From within ArcGIS Pro, Python will act and appear as it did in previous releases of ArcGIS Pro. The switch to conda will impact how you interact with Python (and ArcPy) outside of ArcGIS Pro.

Legado:

In ArcGIS Pro 1.0 through 1.2, if you needed to use Python outside of ArcGIS Pro, you also had to install Python for ArcGIS Pro. With changes at 1.3, this is no longer required. To avoid confusion, uninstall Python for ArcGIS Pro.

Learning Python

A suitable Python reference book is strongly recommended to augment the ArcPy-focused information you find 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 particular 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're new to Python, the external tutorials listed here are recommended for you.

Python tutorials for programmers

The external tutorials listed here are aimed at those who have previous 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 within 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 easily create projects in multiple versions of Python and simplifies the process of installing nearly all publicly available Python packages. Conda is the most popular and widely used Python package manager.

Integrating the functionality of conda into ArcGIS Pro will remove many hurdles faced when writing Python code. It will support installing open-source and third-party libraries that are associated with an individual project, rather than into the base Python installation. This will simplify the process of sharing complex Python tools that will work on multiple computers. Conda does this by providing environments that both isolate installations and solve dependencies.

Learn more about conda in ArcGIS Pro

Running Python

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 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 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.

Using shortcuts

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 .pyfile.

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 within the ArcGIS Pro installation. To configure several common IDEs, see How to set up an IDE or the documentation for the particular IDE.

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

Troubleshooting

In prior versions of ArcGIS Pro, Python was accessed outside of ArcGIS Pro through a separate installation of Python for ArcGIS Pro. Starting in ArcGIS Pro 1.3, Python for ArcGIS Pro is no longer required and should be uninstalled to avoid confusion.

If Python for ArcGIS Pro is still installed, and is used directly, it will be unable to find arcpy and will fail with the following exception:

Traceback (most recent call last):
  File "d:\temp\runme.py", line 1, in <module>
    import arcpy
ImportError: No module named 'arcpy'

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 inside 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 will be provided indicating the lack of permissions. Close the command window, reopen, and Run as administrator.

Authorizing 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 true.

  • Sign me in automatically is checked on when signing in to ArcGIS Pro.
  • ArcGIS Pro is currently open.
  • ArcGIS Pro has been authorized to work offline.
Nota:

If one of the above conditions are not met, you will encounter a RuntimeError: NotInitialized exception when importing arcpy or arcgisscripting.

Learn more about licensing ArcGIS Pro

Temas relacionados