Confluence Retirement

In an effort to consolidate USGS hosted Wikis, myUSGS’ Confluence service is scheduled for retirement on January 27th, 2023. The official USGS Wiki and collaboration space is now SharePoint. Please migrate existing spaces and content to the SharePoint platform and remove it from Confluence at your earliest convenience. If you need any additional information or have any concerns about this change, please contact myusgs@usgs.gov. Thank you for your prompt attention to this matter.

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents
maxLevel3
separatorpipe

What is the Anaconda distribution?

Anaconda is an open-source Python distribution that makes is possible to easily install and manage many pre-packaged third party Python modules.

...

  • Very useful tools not in Esri's standard distribution are available (IPython, Jupyter notebook, Spyder)
  • You can install and update the Python stack as a regular (non-pr) user
  • You can install and remove, and update third party packages easily, with Anaconda checking for compatibility for you 
  • Manage multiple environments (for example, one compatible with ArcMap, another compatible with ArcGIS Pro)

Workflow

The general workflow to make this happen is to:

  • Install Anaconda without fouling the Windows environment (paths, registry) to break Esri's python stack
  • Configure Anaconda with the particular add-ons you want, and
  • Configure ArcGIS's Python so that it is aware of the Anaconda environment (so you can import modules not in Esri's stack)

1) Download and Install Anaconda

The main distribution is pretty large. MiniConda downloads what you need to do to start. Choose the Miniconda that matches the main software you want to integrate with. (You can install both if you want.)

...

At this point, you  have a nice Anaconda Python setup (~ 225 MB) that's spiffy and new and totally useless with ArcMap.

2) Set up an SSL certificate for Conda

The Department of Interior is now requiring SSL encryption. This means you cannot download Anaconda packages without an SSL certificate in place.

...

Code Block
languagetext
mkdir %USERPROFILE%\.certificates
copy %USERPROFILE%\Downloads\DOIRootCA.crt %USERPROFILE%\.certificates
conda config --set ssl_verify %USERPROFILE%\.certificates\DOIRootCA.crt

3) Configure Anaconda To Work with ArcGIS

The following workflow will demonstrate how set up a custom Python environment within Anaconda that has the same modules installed as ArcGIS Python, and then add a compatible version of Spyder and Jupyter.

The following example is for ArcGIS 10.4.1, assuming you have successfully installed MiniConda 2 32-bit (as above).

A. Find the versions of numpy and matplotlib for your version of ArcGIS Desktop

The critical modules for ArcGIS compatibility can be determined from your Desktop or Pro Python command line.

...

Code Block
languagepy
# pyversions.py - report critical python stack for use with ArcGIS
# example output:
# ArcGIS install folder: C:\ArcGIS\Desktop10.4
# sys.executable: C:\Python27\ArcGISx6410.4\pythonw.exe
# matplotlib: 1.4.3
# numpy: 1.9.2
# scipy: 0.15.1
import sys
import os
ff = "{}: {}"
try:
    print("ArcGIS install folder: {}".format(os.environ["AGSDESKTOPJAVA"][:-1]))
    print(ff.format("sys.executable", sys.executable))
    import matplotlib
    print(ff.format("matplotlib", matplotlib.__version__))    
    import numpy
    print(ff.format("numpy", numpy.__version__))
    import scipy
    print(ff.format("scipy", scipy.__version__))
except:
    pass

B. Create an Anaconda environment for use with ArcGIS

We'll also include a few other modules that we know are shipped in the ArcGIS Python stack.

...

    • Determine that the specified package versions are compatible with each other.
    • Find any packages on which these depend and determine most recent versions that are compatible with what you've asked for, if not completely specified.
    • Show you what it plans to do and prompts you to continue.
    • Download (only) the packages you need to "overlay" onto the base environment to get the environment you have specified.
    • Set up a environment subdirectory, (D:\Users\cprice\Miniconda2\envs\arc1041 in our example) installing the downloaded packages into it. This adds up to about 1.5 GB in our ArcGIS 10.4.1 example.

C. Test the virtual environment

Open an Anaconda command window and load the virtual environment.

...

Code Block
languagetext
C:\Users\cprice> conda info --envs
# conda environments:
#
arc1041                  D:\Users\cprice\Miniconda2\envs\arc1041
root                  *  D:\Users\cprice\Miniconda2
D:\Users\cprice>activate arc1041
Activating environment "arc1041"...
[arc1022] D:\Users\cprice> conda list
# packages in environment at D:\Users\cprice\Miniconda2\envs\arc1041:
#
dateutil                  2.4.1                    py27_0
matplotlib                1.3.0                np17py27_0
numpy                     1.7.1                    py27_3
...

D. Add more packages

You can add more packages using conda install, but make sure you specify version numbers for these that won't change the Python modules required to stay compatible with ArcGIS's Python stack.

...

http://deparkes.co.uk/2015/01/29/install-shapely-on-anaconda/

4) Configure ArcGIS python to see Anaconda environment and vice versa

This can most easily be done (personal opinion) with a Python usercustomize.py startup script

...

  • IMPORTANT - Test and debug this script by running it from python.exe for each of your environments. (See examples in the script header.)
  • Place the script in your user site packages folder with the name usercustomize.py
    The user site-packages folder path can be found with:  python -m site --user-site
    Usually:  C:\Users\username\AppData\Roaming\Python\Python27\site-packages (ArcGIS Desktop)
           C:\Users\username\AppData\Roaming\Python\Python34\site-packages (ArcGIS Pro, Python35 for Pro 2.0)

Testing and Troubleshooting

ArcGIS

  1. StartDesktop or Pro, open the Python window
  2. print ("\n".join(sys.path))  -- you should see the Anaconda site-packages near the end of the list

...

  1. Start Anaconda 32-bit or 64-bit  command prompt
  2. activate arc1041  (or whichever environment in the list)
  3. import arcpy
  4. print ("\n".join(sys.path)) -- you should see the ArcGIS site-packages near the end of the list

References

http://www.continuum.io/blog/conda

...