I am trying to install a third-party Python package from within a Python script so that other users can run the script and the package can be installed and loaded without having to manually download and install.
This is the code I'm using:
import subprocess
def install(package):
subprocess.call([sys.executable, "-m", 'pip', 'install', package])
install('pyodbc')
import pyodbc
This seems to work fine from within my stand-alone Python script. However, when I try to run it as a script tool set up in ArcGIS Pro, it is not finding the installed package and is failing with this error message:
ModuleNotFoundError: No module named 'pyodbc'
Do I need to change a path or environment setting?
2 Answers 2
In your python code, you can install a package using
import pip
pip.main(['install','package-name'])
Write this at top of your code.
In 2.3.1 at least, you can install packages by going to "Python" in the starting menu and clicking "Add Packages". (I checked, 'pyodbc' is in the list of available packages.) This feature may be new, I haven't been using arcGIS for very long.
conda install is also a good option, if what you're looking for isn't in the list of available packages in the Package Manager. Just make sure you've got the right environment.
Explore related questions
See similar questions with these tags.
sys.executable
will not point to apython.exe
, it will point toarcgispro.exe
, which will not understand the command line flags intended forpip
. You will have to import pip and use it as shown in gis-professional's answer.