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.

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.

This entry was posted in Python. Bookmark the permalink.

Leave a Reply

5 Comments

  1. Paul Dodd says:

    To import scripts from a common location (somewhere other than your current path), which is useful in cases where you may not have permissions to install packages or you just simply want a spot to put your components without altering the existing Python environment. Use the following:

    sys.path.insert( , )
    import

    Setting the to Zero places your path in front of any existing paths.

    • Paul Dodd says:

      Try this again without the HTML…

      Use the following:

      sys.path.insert( index, pathString)
      import MyCustomScript

      Setting ‘index’ to Zero places your path in front of any existing paths.

  2. ahaigh says:

    Very interesting, however this doesn’t work if you publish a Python Tool from a Python Toolbox to ArcGIS Server.

    • vboycheva says:

      Thanks, Paul! This approach works, even when publishing to ArcGIS Server. However, it requires opening the published script in Python IDE and manually adding these two lines. The published scripts are buried deeply here:

      C:\arcgisserver\directories\arcgissystem\arcgisinput\\…\extracted\v101\

  3. johnmdye says:

    How would this work with a Python Addin. Would I install the module in the addin’s Data Folder or should it be in the addin’s root folder? I would imagine the data folder, but then how to import that module into the script to leverage it’s functions?