ArcMap

A simple approach for including 3rd party Python libraries with your scripts

Often times, your Python scripts require 3rd party Python libraries. For example, the sample tools located here: http://www.arcgis.com/home/item.html?id=f3d91b8f852042e289e09a7ec8342431 requires xlrd, xlwt, and openpyxl Python libraries. This sample includes all of the dependent libraries, and demonstrates just how easy it is.

The secret to most Python modules and packages: they’re just files. You can copy any Python library  into the same folder as the .py file that imports them and it will just work (Note: in the case of C extensions and compiled bytecode, the Python version and architecture must match). Here is the layout of the folder with the libraries we mentioned before:

Finding the install location of a library

Usually the Python library is located in the site-packages folder within the Python install directory, however, if it is not located in the site-packages folder and you are uncertain where it is installed, here is a Python sample to locate Python modules installed on your computer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def find_module(modulename, filename=None):
    """Finds a python module or package on the standard path.
       If a filename is specified, add its containing folder
       to the system path.
       Returns a string of the full path to the module/package."""
    import imp
    import sys
    import os
    full_path = []
    if filename:
        full_path.append(os.path.dirname(os.path.abspath(filename)))
    full_path += sys.path
    fname = imp.find_module(modulename, full_path)
    return fname[1]

Note that Python modules can be individual files (with the extensions .py, .pyc, .pyd, .zip) or directories (which contain a __init__.py to mark them as Python packages). Once you’ve located the file/folder, copy it in. Make sure the name matches up to what you expect it to be named on the import line: if you have a module with a folder named PYTHISLIB but expect to import it as thislib, rename the directory once you’ve copied it in to thislib.

CAUTION:
Before distributing any 3rd party Python library, be sure to understand how it is licensed. If a library is licensed as GPL, and your solution is a commercial product, it may not be possible to distribute that library as part of your product.

Next Article

Basemap Releases Include Over 300 New and Updated Communities

Read this article