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 geoprocessing tool Analyze Tools For Pro 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 Python 2to3 utility to report potential migration issues as well as identify tools and functionality that have not been 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 the list of tools not supported in ArcGIS Pro.

The arcpy.mapping module has been removed and has been replaced with an 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.

Upgrade to Python 3

ArcGIS Pro uses Python 3. This is a significant change from other ArcGIS products, including ArcGIS Desktop, ArcGIS Server, and ArcGIS Engine, that have used (and will continue to use) releases in the Python 2 line. The Python 3 releases are a different line that is incompatible with the Python 2 line. While fundamentally much of the language is the same, many key details have changed considerably, including how strings and dictionaries work and how the standard library is organized.

Python 3 has been in active development for many years, coexisting with Python 2 as the Python community has ported over. At this time, the majority of key third-party libraries have ported over to Python 3, and in the Python community, the consensus is that all new development projects will be done in Python 3.

What do I need to get my scripts to work in ArcGIS Pro and Python 3?

A key decision point will be whether you will update your Python code to work in both 10.x and ArcGIS Pro, or just ArcGIS Pro. If you are doing a one-way conversion of code from Python 2 to Python 3, there is a 2to3 command line utility that can be used to automate much of the process. This utility is available with both Python 2 and 3. It is important to note that 2to3, while an excellent tool, is not a complete solution (it is sometimes estimated as a 95 percent solution), and additional changes may be necessary. If you need to support Python 2 and 3, see below for strategies to support both versions.

Keep in mind that simpler Python scripts may not need any changes at all and may run well without changes.

The sections that follow discuss some of the more common differences between Python 2 and 3 that you are likely to encounter. In addition, an excellent reference for porting to Python 3 and migration strategies is in Lennart Regebro's Porting to Python 3 at the http://python3porting.com/ website. Remember, it is possible to write scripts that will run in Python 2 and Python 3.

Standard library reorganization

As described in Python PEP 3108, Python 3 includes a significant reorganization in 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 of the more common causes 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 your 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() where applicable. iteritems has been removed in Python 3 and items behaves like iteritems used to. The same applies to range and xrange: xrange has been removed from Python 3 and range now behaves like 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 will implicitly turn 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

Consider the io module. It 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.