Programmatically access user's Esri Python path

1304
3
10-07-2020 01:25 PM
DavidLaMartina
New Contributor III

I'm creating a CoreHost app that needs to be able to call ArcPy scripts. To do this, we need (from what I understand) to know the local machine's python path - specifically the path of the python .exe installed with Arc Pro.

I've accomplished this on my development machine by just hardcoding the path to the ArcGIS directory in Program Files - but how can we get the appropriate executable without knowing where and how the user has installed Arc Pro?

0 Kudos
3 Replies
Wolf
by Esri Regular Contributor
Esri Regular Contributor

You can use the registry path HKEY_LOCAL_MACHINE\SOFTWARE\ESRI\ArcGISPro\PythonCondaRoot to retrieve the python root folder used by Pro.

DavidLaMartina
New Contributor III

Thank you Wolfgang Kaiser‌. For others interested, this is the code I used to grab the path from the registry key Wolfgang provided: 

RegistryKey arcGISProInstallDirectoryKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\ESRI\ArcGISPro");
 object pythonDirectoryKeyValue = arcGISProInstallDirectoryKey.GetValue("PythonCondaRoot");
 string pythonDirectoryPath = pythonDirectoryKeyValue.ToString();

string localExecutablePath = @"Scripts\propy.bat";
 string pythonExeutablePath = Path.Combine(pythonDirectoryPath, localExecutablePath);
DanPatterson
MVP Esteemed Contributor

This works on a local machine, I don't know how you can access that information on another machine

import sys
sys.executable
'C:\\arc_pro\\bin\\Python\\envs\\arcgispro-py3\\python.exe'‍‍‍

In my  case, arcgis pro is installed in the c:\arc_pro folder

Addendum 

which yields the same as the registry if you parse the \\envs... onward off


... sort of retired...
0 Kudos