
1.5 Working with Python 3 and arcpy in ArcGIS Pro
Now that we’re all warmed up with some Python, we’ll start getting familiar with Python 3 in ArcGIS Pro by exploring how we write code and deploy tools just like we did when we started out in GEOG 485.
We’ll cover the conda environment that ArcGIS Pro uses in more detail in Lesson 2, but for now it might be helpful to think of conda as a box or container that Python 3 and all of its parts sit inside. In order to access Python 3, we’ll need to open the conda box, and to do that we will need a command prompt with administrator privileges.
Since version 2.3 of ArcGIS Pro, it is not possible to modify the default Python environment anymore (see here for details). If you already have a working Pro + PyScripter setup (e.g. from GEOG 485) and it is at least Pro version 3.1, you can keep using this installation for this class. Else I'd recommend you work with the newest version, so you will first have to create a clone of Pro's default Python environment and make it the active environment of ArcGIS. In the past, students sometimes had problems with the cloning operation that we were able to solve by running Pro in admin mode.
Therefore, we recommend that before performing the following steps, you exit Pro and restart it in admin mode by doing a right-click -> Run as administrator. Then go back to "Project" -> "Python", click on "Manage Environments", and then click on "Clone Default" in the Manage Environments dialog that opens up. Installing the clone will take some time (you can watch the individual packages being installed within the "Manage Environments" window, and you may be prompted to restart ArcGIS Pro to effect your changes); when it's done, the new environment "arcgispro-py3-clone" (or whatever you choose to call it - but we'll be assuming it's called the default name) can be activated by clicking on the button on the left.
Do so and also note down the path where the cloned environment has been installed appearing below the name. It should be something like C:\Users\<username>\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone. Then click the OK button.
Important: In the past, the cloned environment would most likely become unusable when you update Pro to a newer main version (e.g. from 2.9 to 3.x or 3.1 to 3.x). Once you have cloned the environment successfully, please do not update your Pro installation before the end of the class, unless you are willing to do the cloning again. There is a function in V3.x and later of Pro that tries to update your active environment, but might not always work as expected and can leave your curated environments invalid.
Now back at the package manager, the new Python environment should appear under "Active Environment" (where arcgispro-py3 is shown in the figure below). This might take 15+ minutes so you'll need to be patient while the dependencies resolves and installs. You can hover over the process and it will open a command window that you can watch as it works through everything.
The package manager will show you a list of packages that will have to be installed and ask you to agree to the terms and conditions. After doing that, the installation will start and probably take a while. You may also get get a "User Access Control" window popup asking if you want conda_uac.exe to make changes to your device; it is OK to choose Yes.
The next step is to map your IDE to the python.exe in the cloned environment. Each IDE is a little different and if you are unsure how it is done, there are instructions found on the web by doing a simple search 'Map python.exe to <your ide>'. Use the path you noted in the earlier step to map to your cloned environment.
Next up, we'll go over creating a script tool in Pro.