As Python continues its dominance as the defacto scripting language for GIS software, we hit a point when one Python environment isn’t enough. Even if we try to keep a clean system, ArcGIS applications need to install their version(s) of Python. ArcGIS Desktop has two flavours alone, and a third if we include ArcGIS Pro (which can have many itself). How do Desktop users open a .py file in the desired version of Python? Can we make this a little easier? If you don’t mind updating the Windows Registry, some little tweaks can make a big difference and save you some hassle.
To preface, this article is for “GIS Desktop power users” with multiple ArcGIS Python versions that want to create/run scripts on their local machine. If installing Python independently, this article isn’t for you. Great package managers like Conda (Anaconda/Miniconda) exist for these purposes. Conda makes managing multiple environments a breeze - and cross-platform too. If you are a more dedicated ArcGIS developer and using the ArcGIS API for Python, you might still want to consider Conda. If you just need ArcGIS Desktop and/or ArcGIS Pro and want to open/edit a specific version of Python to run some code, keep reading.
ArcGIS and Python
ArcGIS for Desktop software (ArcMap and ArcCatalog) are tied to Python v2.x. The initial install of Desktop will install the 32-bit version since ArcGIS Desktop is… 32-bit. Desktop Background Geoprocessing installs the 64-bit version of Python. Both 32/64-bit will run 99% of v2.x scripts without issue. There are a few Esri tools that fail if run in the wrong version which is why I started down this path in the first place.
ArcGIS Pro on the other hand is based on Python 3.x 64-bit. If only using Pro, you shouldn’t have any issues. In this case, you can even manage additional Python environments directly in ArcGIS Pro! Yup, ArcGIS Pro has a Python Package Manager built-in (err, you guess it - Conda is integrated into Pro). Side note: GDAL is also installed with Pro - a topic for another time.
Selecting the right IDLE/IDE
At this point, we have three Python environments installed. Setting a default can be done as an environment variable but switching between them isn’t as simple. If wanting to select a specific environment we can take advantage of a small registry update. For this example, we will use the installed IDLE application but the same approach can be used for the editor/IDE of your choice.
Don’t screw up the registry. You’ve been warned. If you don’t understand what you are doing, this isn’t the sandbox to learn in. Try it out on a virtual machine, or ex-friends’ computer first.
To add options to the right-click menu, we just need to update the CLASSES registry and file name extension information. This info is stored in three places:
- HKEY_LOCAL_MACHINE (HKLM)
- HKEY_CURRENT_USER (HKCU)
We should never touch HKEY_CLASSES_ROOT directly as it provides a view of the registry that merges the information from these other two sources. HKEY_CLASSES_ROOT also provides this merged view for applications designed for historic versions of Windows.
Where we update depends on who we want this to work for. HKLM if we want these changes to show for all users - or HKCU for the current user only. If you accidentally write values to a key under HKEY_CLASSES_ROOT and the key already exists under HKCU\Software\Classes, the system will store the information there instead of under HKLM\Software\Classes.
First, we should confirm the
HKLM\SOFTWARE\Classes\Python.File\shell section exists. It is created when you install Python or ArcGIS for the first time but worth a quick check. From there we will add all options that don’t already exist and/or can tweak the naming of existing. The name of the keys inside will be the name shown in the context menu (r-click). To each key, we add a command sub-key to provide the IDLE environment reference.
The final results will look similar to this:
Here are all three ArcGIS IDLE environment settings for the Registry assuming the default install directories were used:
Python v3 64-bit installed with ArcGIS Pro
[HKLM\SOFTWARE\Classes\Python.File\shell\Edit with IDLE (ArcGIS Pro)] [HKLM\SOFTWARE\Classes\Python.File\shell\Edit with IDLE (ArcGIS Pro)\command] : @="\"C:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\pythonw.exe\" \"C:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\Lib\\idlelib\\idle.pyw\" -e \"%1\""
Python v2 32-bit installed with ArcGIS Desktop
[HKLM\SOFTWARE\Classes\Python.File\shell\Edit with IDLE (v2 x32)] [HKLM\SOFTWARE\Classes\Python.File\shell\Edit with IDLE (v2 x32)\command] : @="\"C:\\Python27\\ArcGIS10.7\\pythonw.exe\" \"C:\\Python27\\ArcGIS10.7\\Lib\\idlelib\\idle.pyw\" -e \"%1\""
Python v2 64-bit installed with ArcGIS Desktop Background GP
[HKLM\SOFTWARE\Classes\Python.File\shell\Edit with IDLE (v2 x64)] [HKLM\SOFTWARE\Classes\Python.File\shell\Edit with IDLE (v2 x64)\command] : @="\"C:\\Python27\\ArcGISx6410.7\\pythonw.exe\" \"C:\\Python27\\ArcGISx6410.7\\Lib\\idlelib\\idle.pyw\" -e \"%1\""
At this point, we could add more options for additional IDE’s/editors (VS Code, PyCharm, Atom, NPP), or start scripting. Don’t forget to backup your registry before you start just in case.
Cover image: CC-BY-SA Opensource.com (https://opensource.com/article/19/5/python-3-default-mac) Oct 14, 2020.