Python migration from 10.x to ArcGIS Pro

How you use Python with ArcGIS Pro is different from other ArcGIS products, including ArcGIS Desktop, ArcGIS Server, and ArcGIS Engine.

The Analyze Tools For Pro tool can be used to highlight issues that would prevent Python code or Python-based geoprocessing tools from running. The Analyze Tools For Pro tool uses the third-party Python fissix module to identify potential migration issues. The tool also identifies tools and functionality that have not migrated to ArcGIS Pro.

Changes to functionality in ArcPy

The majority of geoprocessing tools will continue to be available in ArcGIS Pro; however, some will not. Tools that are not included are those in the Coverage, Parcel Fabric, Schematics, and Tracking Analyst toolboxes. Additional tools in other toolboxes are also not available. For a full list, see Tools that are not available in ArcGIS Pro.

The arcpy.mapping module has been removed and has been replaced with the arcpy.mp module in ArcGIS Pro to support mapping workflows in ArcGIS Pro.

The arcpy.na module also includes changes, many related to the change from arcpy.mapping to arcpy.mp.

Python 3

ArcGIS Pro uses Python 3. Python 3 is a popular programming language that is used in a variety of fields including data analysis, data science, quality assurance, and web and software development.

Legacy:

Earlier ArcGIS products, including ArcGIS Desktop, ArcGIS Server, and ArcGIS Engine use versions of Python 2. The Python 3 releases are a different line that are only partially compatible with Python 2. While fundamentally much of the language is the same, many details have changed, including how strings and dictionaries work and how the standard library is organized.

Convert an existing script to use in ArcGIS Pro and Python 3

If you are doing a one-way conversion of code from Python 2 to Python 3, you can use the fissix module to automate much of the process. The fissix module is a modernized backport of the deprecated Python lib2to3 library.

Simpler Python scripts may not need changes at all and may run well without them.

The sections that follow describe some of the common differences between Python 2 and 3 that you are likely to encounter.

Standard library reorganization

Python 3 includes a reorganization of the standard library from Python 2. Most of these changes involve functionality that has been moved to a different location or is part of a module that has been renamed. To write code that works in both Python 2 and 3, these differences can be handled using flexible imports such as the following for modules such as urllib2:


try:
    import urllib2  # Python 2
except ImportError:
    import urllib.request as urllib2  # Python 3

print statement and function

One common cause of syntax errors in Python 2 is the conversion of the print statement to the print function. Fortunately, the print function has been backported to Python 2 and can be safely used in Python 2 and 3.


print('This will print safely in Python 2 and 3')

# print statements will only work in Python 2
# print 'Print statements will only print safely in Python 2'

__future__

At the top of your scripts, import the __future__ flags to make the Python 2 code subject to some of the new Python 3 rules. Once your scripts correctly parse with this line, you are well on your way to Python 3 compatibility.


from __future__ import print_function, unicode_literals, absolute_import

Dictionaries

Use dictionary.items() rather than dictionary.iteritems() when applicable. iteritems has been removed in Python 3, and items behaves in the same way as iteritems. The same applies to range and xrange: xrange has been removed from Python 3, and range now behaves in the same way as xrange.

Strings and encoding

In Python 3, anything in quotation marks is now implicitly a Unicode string. The byte array takes the place of the old str type. The unicode_literals import implicitly turns everything within quotation marks in Python 2 to Unicode.

Python 3 is stricter in how it handles character encoding. Explicitly save your scripts as UTF-8 if you haven't already. Add a coding line such as # coding: utf-8 at the top of your script so Python can identify that the script is in UTF-8.

io module

The io module was introduced in Python 2.6 and is included with Python 3. It provides a clean way of handling many input and output tasks in a text encoding-aware manner.

Unsupported data formats

Several data formats are not currently supported in ArcGIS Pro, including raster catalogs, geometric networks, schematic datasets, geodatabase servers, ArcMap document templates, ArcReader documents, graphs, tiled map packages, and personal geodatabases.