Get ArcPy to Work with Anaconda – Installation and Troubleshooting

anacondaarcpyimporterrorinstallation

I am trying to follow the step-by-step directions in the answer to "how to separate Python installation with ArcGIS" but cannot find the path file, DTBGGP64.pth, in C:\Python27\ArcGIS10.2\Lib\site-packages.

Has someone has faced a similar issue?

I am wondering if there is a workaround by including a path file that tells Anaconda where arcpy is installed? I tried to create a .pth file containing the following text:

# .pth file for arcpy

C:\Program Files (x86)\ArcGIS\Desktop10.2\bin 
C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy
C:\Program Files (x86)\ArcGIS\Desktop10.2\Scripts

and saved it to C:\Anaconda\Lib\site-packages (that is, if this is indeed where all the packages are saved) but I get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\__init__.py", line 21, in <module>
    from arcpy.geoprocessing import gp
  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing\__init__.py", line 14, in <module>
    from _base import *
  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing\_base.py", line 14, in <module>
    import arcgisscripting
ImportError: DLL load failed: %1 is not a valid Win32 application.

I am running conda 3.7-py27 on a windows 7 (64bit) platform. Anaconda is installed in C:\Anaconda and ArcMap is installed in C:\ArcGIS\Desktop10.2. Is there an alternative solution to get arcpy to work with Anaconda? I just want to be able to run my scripts from Powershell.

Best Answer

This tutorial shows you how to set up conda environment to work with ArcGIS 10.4 and ArcGIS Pro 1.3.

Workflow to set up Anaconda with ArcGIS 10.4

  • 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 appropriate Anaconda content.

1) Install Anaconda for All Users

  1. Go to http://continuum.io/downloads
  2. Download the 32-bit version of Anaconda (Python 2.7)
  3. In the install dialogs:
    • Select install for All Users
    • Install to a folder by default (C:\Anaconda2)
    • IMPORTANT: To avoid breaking ArcGIS (or other software), uncheck the checkboxes (a) make Anaconda the default Python and (b) add Anaconda's Python to the PATH.
  4. Go to Start > All Programs(apps) > Anaconda2(32-bit) > Anaconda Prompt. Right click, run as administrator

2) Configure an Anaconda environment for use with ArcGIS

  1. Find the versions of numpy and matplotlib ArcGIS is using.
Open ArcMap and its Python window, and enter these commands:

    • >>> import sys, numpy, matplotlib
    • >>> print(sys.version, numpy.__version__, matplotlib.__version__)
    • ('2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)]', '1.9.2', '1.4.3')
  1. Create an Anaconda environment that is compatible with ArcGIS
    • Get to the Anaconda Command Prompt (Start > All Programs(apps) > Anaconda2(32-bit), pick "Anaconda Prompt"), Right click, Run as Administrator.
    • Type (depending on ArcGIS version, I am using ArcGIS 10.4 as an example here):
    • "conda create -n arc104 python=2.7.10 numpy=1.9.2 matplotlib=1.4.3 pyparsing xlrd xlwt pandas scipy ipython ipython-notebook ipython-qtconsole"
    • Enter y to proceed.
    • Anaconda's conda command will then set up an environment subdirectory, ex: " C:\Anaconda2\envs\arc104 ", installing the downloaded packages into it.
  2. Test the virtual environment
    • At the Anaconda Command Prompt, type: activate arc104
    • Type: conda list. You can see the list of packages installed.
  3. 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 environment's version of python or numpy (or ArcGIS will not be able to use that environment anymore).
    • Let's add the Python Spatial Analysis Library (pysal) module.
    • Type the following command at the Anaconda Prompt:"conda install -n arc104 python=2.7.10 numpy=1.9.2 matplotlib=1.4.3 pysal"

3) Configure ArcGIS to see Anaconda and vice versa

  1. Anaconda Python to ArcPy
    • Copy the Desktop10.4.pth file to the Anaconda environment site-packages folder:
    • From: C:\Python27\ArcGIS10.4\Lib\site-packages\Desktop10.4.pth
    • To: C:\Anaconda2\envs\arc104\Lib\site-packages\Desktop10.4.pth
  2. Arcpy to Anaconda Python
    • Create a zconda.pth (path) file with the content "C:\Anaconda\envs\arc104\lib\site-packages" in it.
    • Then copy zconda.pth to C:\Python27\ArcGIS10.4\Lib\site-packages
  3. Testing in ArcMap
    • As a regular user, start ArcMap, open the Python window
    • type "import pysal"
    • type "pysal." A popup menu with a list of pysal-provided functions is a pretty good sign the installation succeeded.
  4. Testing in PyCharm
    • Start PyCharm, in File\Settings…, choose Project then Project Interpreter
    • Ignore the drop down list for Project Interpreter, and click the cog button to Add Local, and in the file browser pick C:\Anaconda2\envs\arc104\python.exe
    • To run your script, right click it in the Project window, and choose either Run or Debug
    • Restart PyCharm for the Python Console to use the arc104 environment.

Workflow to set up Anaconda with ArcGIS Pro 1.3

1) Create an Anaconda environment that is compatible with ArcGIS Pro

  • Copy the folder arcgispro-py3 from C:\Program Files\ArcGIS\Pro\bin\Python\envs and paste to C:\Anaconda2\envs
  • Rename the copied folder arcgispro-py3 in C:\Anaconda2\envs to arcpro

2) Test the virtual environment

  • At the Anaconda Command Prompt, type: activate arcpro
  • Type: conda list. You can see the list of packages installed

3) Add more packages

  • Let's add the Python Spatial Analysis Library (pysal) module.
  • Type the following command at the Anaconda Prompt:"conda install pysal"

4) Configure ArcGIS to see Anaconda and vice versa

  • Anaconda Python to ArcPy
    • Edit the ArcGISPro.pth (path) file within "C:\Anaconda2\envs\arcpro\lib\site-packages".
    • Change the relative ArcPy path to C:\Program Files\ArcGIS\Pro\Resources\ArcPy
    • Change the relative ArcToolBox path to C:\Program Files\ArcGIS\Pro\Resources\ArcToolBox
  • Arcpy to Anaconda Python
    • Create a zconda.pth (path) file with the content "C:\Anaconda2\envs\arcpro\lib\site-packages" in it.
    • Then Copy zconda.pth to C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages
  • Testing in ArcGIS Pro
    • Start ArcGIS Pro, open the Python window
    • type "import pysal"
    • type "pysal." A popup menu with a list of pysal-provided functions is a pretty good sign the install succeeded.
  •  Testing in PyCharm
    • Start PyCharm, in File\Settings…, choose Project then Project Interpreter
    • Ignore the drop down list for Project Interpreter, and click the cog button to Add Local, and in the file browser pick C:\Anaconda2\envs\arcpro\python.exe
    • To run your script, right click it in the Project window, and choose either Run or Debug
    • Restart PyCharm for the Python Console to use the arcpro environment.

References

  1. https://gisday.wordpress.com/2016/07/18/setting-up-anaconda-pysal-with-arcgis-python-environment/
Related Question