1

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?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jul 9, 2018 at 19:07
4
  • 5
    Which version of ArcGIS Pro? Things changed at 2.2 in regards to the Python environment and Conda. I'd read: pro.arcgis.com/en/pro-app/arcpy/get-started/what-is-conda.htm Commented Jul 9, 2018 at 19:33
  • I'm actually still on 2.1.3. I have not upgraded to 2.2 yet. But, this is helpful for understanding the package manager better. Commented Jul 10, 2018 at 12:46
  • Did you ever find a solution to this? I'm looking to do the same in Pro 2.3.1 Commented Mar 28, 2019 at 14:54
  • From within ArcGIS Pro, sys.executable will not point to a python.exe, it will point to arcgispro.exe, which will not understand the command line flags intended for pip. You will have to import pip and use it as shown in gis-professional's answer. Commented Mar 28, 2019 at 18:20

2 Answers 2

1

In your python code, you can install a package using

import pip
pip.main(['install','package-name'])

Write this at top of your code.

answered Mar 28, 2019 at 18:15
0

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.

answered Mar 28, 2019 at 18:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.