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 += 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.

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 and tagged . Bookmark the permalink.

Leave a Reply


  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( , )

    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:


  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?

  4. mbakerdps says:

    Does the ‘python libraries are just files’ secret include arcpy?

    I’m using a standard python installation rooted at c:\python27 and using the pypyodbc and psycopg2 libraries. I’m hoping to deploy some arcpy scripts on the same server running the other scripts. When I install arcgis desktop, my sys.path variables are all overwritten to the ArcGIS python paths…

    So is this article suggesting that I should only use the ArcGIS python installation and ‘move’ my pypyodbc and psycopg2 libraries into the arcgis installation?

  5. eggerikt says:

    Thank you for this how-to. I am hoping to find some guidance on how to utilize the PyPDF2 module that now comes with ArcGIS Pro from within a script that gets executed from ArcMap. I can import PyPDF2 from inside of Pro, but the module is not recognized inside of ArcMap. I would like to incorporate it in scripts that get used on multiple computers around the office (so local-machine solutions aren’t ideal).

  6. hlzhang525 says:

    Great job, jpardy,
    Be very helpful to advise us on how to copy /install the package “Scikit-learn” into the Python folder for ArcGIS 10.4/ Pro 1.3 or later?
    We are struggling to do that for a while ….

    Thanks a lot in advance!

  7. hlzhang525 says:

    One more question,
    Can you explain the vision of Conda support for ArcGIS Pro 1.3/1.4 (https://arcpy.wordpress.com/2016/10/21/conda-and-arcgis-pro/ )? What about ArcGIS 10.5?