0% found this document useful (0 votes)
17 views10 pages

Migrate Python 2 Scripts to 3 Guide

Uploaded by

bright ezenwa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views10 pages

Migrate Python 2 Scripts to 3 Guide

Uploaded by

bright ezenwa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Exercise 8: Chapter 8

Migrating scripts from Python 2 to 3

Exercise data
Exercise data for this book can be downloaded from [Link]

This is a link to the ArcGIS Online group named Python Scripting for ArcGIS Pro (Esri Press). The

data for exercise 8 is posted as a ZIP file named PythonProAdv_Ex08.zip. Download this file and

extract it to a folder of your choice. The instructions use the folder C:\PythonProAdv, but you

can use a different folder provided you update any paths.

Using the Python 2to3 program


There are many differences between Python 2 and Python 3. To facilitate migrating

scripts, you can use the Python 2to3 program. Next, you will take a standard testing script and

explore how the 2to3 program works.

1. Start IDLE.

2. Create a new script file and save your script as test_script.py to the

C:\PythonProAdv\Ex08 folder.

3. Enter the following lines of code:

def greet(name):

print "Hello, {0}!".format(name)

print "What's your name?"


name = raw_input()

greet(name)

This code was written for Python 2 and will produce errors when run using Python 3.

You may be able to identify some of the errors in the script. You also can try running the script

to see what errors occur. This is cumbersome so you will use the 2to3 program to help identify

the errors that need fixing.

4. Close the test_script.py script.

5. Start the Python Command Prompt application.

Note: As a reminder, per earlier exercises, you should use the Python command prompt

installed with ArcGIS Pro. You can navigate to the application called Python Command Prompt

in the ArcGIS Pro program group or search for the application by name.

6. At the command prompt, enter the following code and press Enter:

cd\

7. Enter the following code (you will need to modify this path if you saved your

script to a different folder) and press Enter:


cd C:\PythonProAdv\Ex08

8. Enter the following code and press Enter:

2to3 test_script.py

The result is a series of differences, indicating the lines that must be corrected to be

compatible with Python 3.

Although the differences are printed to the screen, the script is not modified. Next, you

will write the differences to the script file.


9. Enter the following code and press Enter:

2to3 -w test_script.py

This code saves the changes to the script file. A backup is created automatically in the

same folder. The name of this backup file is test_script.[Link]. You can recover the original by

removing the .bak file extension and renaming the script file—e.g., test_script_v2.py. Next, you

will examine the changes to the script file.

10. In IDLE, open the test_script.py file and review the results.

All the changes identified earlier have been made to the script.

11. Run the script.

12. When prompted for your name in the interactive interpreter, type your name

and press Enter.

The result is shown in the following figure.


Although the 2to3 program is useful to migrating scripts from Python 2 to 3, it does not

recognize issues that are specific to ArcPy or ArcGIS Pro, such as tools that have been removed

or data formats that are no longer supported.

Using Analyze Tools for Pro


When migrating scripts, you must account for differences between Python 2 and 3, as

well as for differences specific to ArcPy or ArcGIS Pro. You will use the Analyze Tools for Pro tool

to test custom tools developed for ArcGIS Desktop 10.x for compatibility with ArcGIS Pro.

1. Start ArcGIS Pro with a new blank project.

2. Make sure the Catalog pane is visible by clicking Catalog Pane on the View tab.

Dock the Catalog pane to the right side of the ArcGIS Pro interface.

3. Create a new folder connection to the location of the exercise data by right-

clicking Folders > Add Folder Connection and navigating to the folder—e.g.,

C:\PythonProAdv\Ex08.

4. Expand the Ex08 folder and the Excel subfolder.


The Table To Excel Tool toolbox contains a Python script tool developed for ArcGIS

Desktop 10.x. The Scripts subfolder contains the Python code necessary to run the tool. This is a

copy of the tool named Table To Excel Tool that can be downloaded from [Link].

5. On the Analysis tab, click Tools.

6. In the Geoprocessing pane, search for the Analyze Tools for Pro tool.

7. Open the Analyze Tools for Pro tool.

8. Drag the Table To Excel [Link] file from the Catalog pane into the Input

parameter.

9. For Output File, enter C:\PythonProAdv\Ex08\excel_result.txt.

The tool dialog box is shown in the figure.


10. Click Run.

The tool completes with warnings. You can click View Details to review the warnings, or

you can open the text file.

11. Using File Explorer, navigate to the C:\PythonProAdv\Ex08 folder and open

the excel_result.txt file in Notepad.

The results can be difficult to read because of poor formatting. With a bit of

reformatting, the messages look like the following figure.


The Analyze Tools For Pro tool found several Python 2 to 3 errors, including errors

related to the use of unicode strings and the replacement of the next() method in Python 2

with the next() function in Python 3. Also note that there are errors in the script associated

with the tool ([Link]) but also in the validation code built into the .tbx file.

Note: If you analyzed the [Link] script using the 2to3 program, you would find the

same errors, although the organization of the result is different. The 2to3 program would not

find the errors in the validation code in the .tbx file. The Analyze Tools For Pro tool runs the

2to3 program on this validation code as well because it is designed to work with .tbx and .pyt

files in addition to .py files.

No changes are made to the underlying script. To make the tool compatible with ArcGIS

Pro, you would need to make the suggested changes manually, which is beyond the scope of

this exercise.

The errors identified so far have been limited to difference between Python 2 and 3.

Next, you will analyze a different tool with additional errors.

12. In the Catalog pane, expand the CCMaps folder in the C:\PythonProAdv\Ex08

folder.
The Conditioned Choropleth Maps tool is a Python script tool for multivariate data

analysis. The output is a series of choropleth maps. The tool was developed for ArcGIS Desktop

10.x. The associated script file [Link] resides in the root folder of the tool. This

tool is a copy of the tool named Conditioned Choropleth Maps (CCMaps) Generator, which can

be downloaded from [Link].

13. Analyze the Multivariate Analyst toolbox using the Analyze Tools For Pro tool,

similar to the previous steps. Save the results to a text file named

ccm_result.txt.

After reformatting of the messages, the results look like the following figure.

The results reveal several Python 2 to 3 errors with suggested fixes, as well as several

warnings associated with the [Link] module, which has been replaced with the
[Link] module. No fixes are suggested, and those parts of the code require significant

revisions.

[End of exercise 8.]

Common questions

Powered by AI

To prepare a Python script for use in ArcGIS Pro, first run the 2to3 program to address basic Python 3 compatibility issues. This includes updating syntax such as the print function and replacing deprecated elements. Next, use the Analyze Tools for Pro tool to identify additional issues specific to ArcGIS, such as deprecated modules like arcpy.mapping. Challenges include manual revisions needed for domain-specific issues that 2to3 cannot fix, such as significant code refactoring for compatibility with new ArcGIS Pro modules and formats, which may require deep understanding of the toolboxes involved .

The 2to3 program outputs a list of syntactic differences required for Python 3 compatibility, but it does not alter the script unless explicitly instructed to do so with a flag, and it cannot assess validation codes in toolboxes. In contrast, the Analyze Tools for Pro tool provides a comprehensive analysis that includes errors in toolbox script validation code and domain-specific issues such as deprecated modules. For a migration project, the 2to3 program is useful for initial script adjustments, while the Analyze Tools for Pro tool is crucial for a full transition to ArcGIS Pro by addressing domain-specific concerns and assessing .tbx files .

The Analyze Tools for Pro tool can identify Python 2 to 3 errors, such as the use of unicode strings and the need to replace the next() method with the next() function. Additionally, it finds errors in validation code inherent in .tbx files, which the 2to3 program alone does not address. Furthermore, it provides warnings related to outdated modules like arcpy.mapping, now replaced by arcpy.mp, which require significant manual code revisions. Unlike 2to3, it checks scripts and validation code associated with ArcGIS-specific files like .tbx and .pyt files .

The 2to3 program helps in migrating Python scripts from version 2 to 3 by identifying lines of code that need revisions for compatibility with Python 3. It points out syntactic differences such as changes in the print syntax and input function names. However, it has limitations when used with ArcPy or ArcGIS Pro scripts, as it does not recognize domain-specific issues such as tools that have been removed or data formats no longer supported. For those, manual intervention is needed, as these issues are not covered by 2to3's automated conversions .

The 2to3 tool is limited to detecting syntax changes between Python 2 and 3, such as print function updates and input handling, and does not address domain-specific compatibility issues such as deprecated ArcGIS modules or structural changes in toolboxes. This limitation necessitates the use of additional tools like Analyze Tools for Pro, which can assess these domain-specific elements and ensure comprehensive adaptations to newer ArcGIS versions. Without such tools, scripts might fail due to overlooked GIS-specific changes .

The Analyze Tools for Pro tool enhances compatibility analysis by not only performing 2to3 conversions for Python syntax but also examining ArcGIS-specific validation codes in .tbx and .pyt files. It captures domain-specific errors, such as deprecated ArcGIS modules and tool-specific logic that 2to3 does not address, thereby providing a more comprehensive analysis for scripts transitioning to ArcGIS Pro. This dual capability caters to both Python versions' differences and GIS domain-specific changes .

Reformatting is essential for making the output from Analyze Tools for Pro readable and coherent, as its poor default formatting can obfuscate important information, making it difficult to understand the context and importance of each error or warning. Challenges include distinguishing between minor and critical issues, and ensuring that necessary adjustments are made correctly, which may require further knowledge of GIS software changes to interpret the results effectively .

The Analyze Tools for Pro tool finds several Python 2 to 3 errors in the Conditioned Choropleth Maps tool, including issues with unicode string handling and the next() method needing replacement with the next() function. Additionally, it highlights warnings regarding the use of the arcpy.mapping module, which needs to be replaced with the arcpy.mp module for compatibility with ArcGIS Pro, requiring manual code revisions without suggested fixes .

To manually correct scripts for full compatibility with ArcGIS Pro, you need to review the warnings and errors identified by the Analyze Tools for Pro tool, particularly those concerning deprecated modules like arcpy.mapping, which should be replaced with arcpy.mp. Additionally, unicode string usage needs updating, and method changes from next() to the next() function must be addressed. Manual interventions are essential because the tool only identifies issues and suggests how to fix them without automatically updating the scripts, especially where significant structural changes or domain-specific knowledge are required .

After using the 2to3 program and Analyze Tools for Pro tool, review the suggested changes and carefully modify the script to address any flagged issues, especially those related to deprecated modules like arcpy.mapping, which must be changed to arcpy.mp for ArcGIS Pro. Verify that the updated scripts and any associated validation code function correctly within the new environment. Since manual updates are often required for structural changes, comprehensive testing should be conducted to ensure functionality is preserved and errors are resolved .

You might also like