GTaylor-esristaff

Auto-Installing 3rd Party Libraries in Python

Blog Post created by GTaylor-esristaff Employee on Mar 20, 2017

Requiring users within your organization to manually install python libraries via command-line is often very counter-productive and time consuming. Often these users have minimal to no experience with Python or how to use command-line operations.

 

The following Function can be used in your script in python 3.5 ArcGIS Pro GP tools to Automatically install libraries of interest defined by user in a List called packageList if they are not currently installed on the machine:

 

The first shows auto-installing libraries via PIP:

def load_or_install_libraries(packageList):
    for package in packageList:       
        '''Check if python package is installed and import/install if it does not exist'''
        try:
            __import__(package)
        except ImportError:
            print("Detected {0} module/library not installed. Installing {0}".format(package))
            import importlib
            import pip
            pip.main(['install', package])
            print("{0} Successfully Installed".format(package))
            __import__(package)
##########
# Script #
##########
# List of Python Packages to Install.. replace "Package0" with "yourPackageName"
packageList = ["Package0","Package1"]

load_or_install_libraries(packageList):
# Begin Your Script Operations Here:

 

Secondly... Libraries via Easy_Install:

def load_or_install_libraries(packageList):
    for package in packageList:       
        '''Check if python package is installed and import/install if it does not exist'''
        try:
            __import__(package)
        except ImportError:
            print("Detected {0} module/library not installed. Installing {0}".format(package))
            import importlib
            import easy_install
            easy_install.main(['-U', package])
            print("{0} Successfully Installed".format(package))
            __import__(package)
##########
# Script #
##########
# List of Python Packages to Install.. replace "Package0" with "yourPackageName"
packageList = ["Package0","Package1"]

load_or_install_libraries(packageList):
# Begin Your Script Operations Here:

 

That's all there is to the operation. Hope you find this helpful.

Outcomes