Select to view content in your preferred language

Correct Way to Install Modules from Within GP Tool

505
4
Jump to solution
01-27-2025 08:35 AM
Glasnoct
Regular Contributor

So I've seen a handful of half-solutions to this and the answer either seems incomplete or outdated so I'm trying to nail this down. The end user may not have the required python modules installed (and in some cases those are not available through the package manager menu) so I want to check for and install them when a tool is run to avoid the process of walking them through installing it themselves.

So far I have seen the following methods suggested as working:

pip install module

pip.main(['install', module])

subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--upgrade', module])

my pip version is above 10.0 so the last command is the suggested solution I've seen but sys.executable points to arcpro.exe. It seems to want to install but during the process will continually open new instances of ArcPro and the installation errors out if I just close the new instances. What do I need to replace sys.exectuable with to make this function correctly or is there a better way?

0 Kudos
1 Solution

Accepted Solutions
DavidSolari
MVP Regular Contributor

You can get all the Python exe paths like so:

subprocess.run(["where", "python"], capture_output=True, universal_newlines=True).stdout.strip().split("\n")

If you have multiple paths then find the one with "conda" inside and you can take it from there.

View solution in original post

4 Replies
DavidSolari
MVP Regular Contributor

You can get all the Python exe paths like so:

subprocess.run(["where", "python"], capture_output=True, universal_newlines=True).stdout.strip().split("\n")

If you have multiple paths then find the one with "conda" inside and you can take it from there.

Glasnoct
Regular Contributor

In formulating a follow-up question, I recollected something about propy.bat (I'd been through this issue before several years ago and remember very little about it) so I went and confirmed what I think it would do. Your solution will (assuming, as I do not have two conda environments cloned from the default) that the returned list is going to include both environments' interpreters and provide no way to distinguish which is the active environment for ArcPro. Propy.bat located in (default install) will run provided commands with the active environment automatically, sidestepping that issue.

 

import subprocess
module = 'networkx'
# changed from check_call to check_output so I can see the result
result = subprocess.check_output([r'c:\Progra~1\ArcGIS\Pro\bin\Python\scripts\propy.bat', '-m', 'pip', 'install', '--upgrade', module])
print(result.decode('utf-8'))

 

Seems to work a treat, thanks.

 

 

0 Kudos
DavidSolari
MVP Regular Contributor

For what it's worth, my machine has two user environments on top of the default one and the only python exes my method found was the Windows store one and the activated conda one. Leveraging propy.bat is a clever idea, good thinking.

0 Kudos
DougBrowning
MVP Esteemed Contributor

What I do is have the package on the network then use sys.path.append(path to dir of packages) before the import.  Works slick.  We can't install any packages so your way would not work for us.

0 Kudos