Managing Python Modules Locally without Conda

by | Jul 28, 2017

For ArcMap users, who cannot rely on Conda for Python package management because Python 2.7 is the default version used by ArcMap, installing and managing local Python packages can be a real challenge. This blog post covers some of the available tools that help you manage modules locally.

ArcPy site package updates

While Esri has expanded support for Python through its ArcGIS platform with a Python API and Conda package manager, this doesn´t mean there´s no further development of the arcpy site package. In fact, Esri has plans to offer it as a separate package from Pro version 2.1 to better manage the package dependencies with arcpy and corresponding Pro version. As for the package itself, Esri continues to add new Python modules, such as NetCDF4, xlrd, xlwt, dateutil and pip. If you use ArcMap 10.5, you can list and access all these modules locally by following the instructions below.

Like what you’re reading? Subscribe now and receive the full version of our newsletter delivered to your inbox each week.

Listing available Python modules

If you´re curious to know what Python modules come with a local ArcGIS Desktop installation, you have several options at hand to find out. Of course you can navigate to the lib\site packages folder and have a look there, but these only list the site packages and not the modules that are inside of those site packages. If you know the name of a module you´re looking for, the easiest way to find out if it is installed on your machine is to open an IDE or command prompt and use the import statement and the module name:

>> import netCDF4

If there´s no error message after hitting enter, you´re good to go. If there is an error message, you can use “pip install” to download the module by opening a command prompt window and entering the code below, which will auto-install the requested package:

C:\>pip install PyPDF2

But suppose you’d want a list of all available modules – what options are there? The Python documentation offers little help on this matter, although the following command comes close:

>> help(‘modules’)

You´ll see that Python will create a list of available modules, but it´s incomplete: for example, you´ll see the arcpy site package listed, but without the different modules inside of it. For a list of Python modules with a dot notation (such as arcpy.mapping), a more sophisticated approach is needed. Luckily, Esri provides a Python script called _importable_modules.py that comes with a standard Python install and is stored in your program files folder (in my case C:\Program Files (x86)\ArcGIS\Desktop10.5\arcpy\arcpy\_ importable_modules.py).

Running this script will print a list of all importable Python modules on your system including those with dot notation. This might come in handy if you want to run someone else´s scripts, or want to play around with a certain module and check if it´s is available before importing it. However, close inspection of the output shows that dot notation is not used consistently. For example, the arcpy.da and arcpy.na modules are listed as “da” and “na” – without the use of dot notation , while other modules do follow the correct dot notation, such as arcpy.sa.Utils. It seems this is as close we can get to an overview of all importable Python modules.

Like what you’re reading? Subscribe now and receive the full version of our newsletter delivered to your inbox each week.

Listing the location of a module

If you´re curious to find out the location of one or more modules, simply import an installed module and print its path that will be then returned inside of a list. This is also handy for deleting a package as it shows you the directory you need to delete (you can also use “pip uninstall” followed by a package or module name, run from a command prompt to let Python handle this for you).

>> import xlrd

>> print xlrd.__path__

[‘C:\\Python27\\ArcGIS10.5\\lib\\site-packages\\xlrd’]

An even shorter syntax that also works, but shows the location on an init file instead of only the directory:

>> print xlrd

<module ‘xlrd’ from ‘C:\Python27\ArcGIS10.5\lib\site-packages\xlrd\__init__.pyc’>

Checking if a module exists without importing it

If you open the _importable_modules.py script inside of an IDE, you will see that it imports the imp module, which is a built-in module that can be used to check if a module exists on your machine but without importing it. The code example below can be used to check if a module exists (this code only works for modules without dot notation). If the search is successful, Python returns a 3-element tuple containing a file, pathname and description. In this case, the last element is also a tuple.

>> import imp

>> try:

imp.find_module(‘dateutil’)

found = True

except ImportError:

found = False

(None, ‘C:\\Python27\\ArcGIS10.5\\lib\\site-packages\\dateutil’, (”, ”, 5))

Categories

Recent Posts

Eric van Rees
Eric van Rees is a freelance writer and editor. His specialty is GIS technology. He has more than eight years of proven expertise in editing, writing and interviewing as editor and editor-in-chief for the international geospatial publication GeoInformatics, as well as GIS Magazine and CAD Magazine, both published in Dutch. Currently, he writes about geospatial technology for various clients, publications and blogs.

Sign up for our weekly newsletter
to receive content like this in your email box.