English

How To: Import custom Python modules in ArcGIS Pro

Summary

Some organizations keep a shared repository of custom Python scripts and make them accessible by adding the path to the shared directory to the Windows Python path variable for each client computer running ArcMap. 

For example, \\server\folder\fibo.py contains a function called fib() that returns the Fibonacci sequence. Proper configuration allows for ArcGIS to successfully execute the following:

>>> from fibo import fib
>>> fib(10)

The same functionality can be achieved in ArcGIS Pro with the proper configuration.

Procedure

A .pth file is used by ArcGIS Pro to determine which modules are included. This can be altered to include custom paths, and only needs to be done once (per computer or conda environment).

  1. Open Windows Explorer, and navigate to Program Files > ArcGIS > Pro > bin > Python > envs > arcgispro-py3 > Lib > site-packages > ArcGISPro.pth. Open the file.
  2. Add the following line to the .pth file:
    import sys; sys.path.append(r"\\server\folder\")
    This script can be verified (or troubleshoot the syntax) by entering the following command into Python Command Prompt, or by attempting custom local module imports.
    python -c "import sys; print('\n'.join(sys.path))"
    
  3. Alternatively, append the path to the custom modules to the system path for just the duration of the script. This may be a better approach if custom modules in script tools are only used in ArcGIS Pro.
    myPythonModules = r'\\server\scripts'
    sys.path.append(myPythonModules)
    import myutils # a Python file within myPythonModules

Related Information

https://pro.arcgis.com/en/pro-app/help/analysis/geoprocessing/share-analysis/packaging-python-scripts.htm#GUID-9FE201C7-42D6-443F-929D-4150B1CB1EC2