Getting ArcPy to work with Anaconda?
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
- Go to http://continuum.io/downloads
- Download the 32-bit version of Anaconda (Python 2.7)
- 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.
- 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
- Find the versions of numpy and matplotlib ArcGIS is using.
- >>> 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')
- 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.
- Test the virtual environment
- At the Anaconda Command Prompt, type: activate arc104
- Type: conda list. You can see the list of packages installed.
- 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
- 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
- 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
- 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.
- 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
- https://gisday.wordpress.com/2016/07/18/setting-up-anaconda-pysal-with-arcgis-python-environment/
You're using a 64 bit version of Python in your Anaconda installation and ArcMap's Python is 32 bit. You'll need to install some 64 bit ArcGIS build (either 64 bit Background Geoprocessing or ArcGIS for Server) and point to that instead, or install a 32 bit Anaconda instance instead and try again. See the downloads page and get the 32 bit installer.
To address this, browse to the python27\Lib\site-packages folder, mine was located under "C:\Python27\ArcGIS10.1\Lib\site-packages", and find the file Desktop10.1.pth.
On my system, the problem was resolved by simply copying the Desktop10.1.pth file into "C:\AppData\Local\Continuum\Anaconda\Lib\site-packages".
If you are running Anaconda 64 bit, you will get a different error when you try to import arcpy because ArcGIS runs 32 bit. Therefore, you have to make sure that you have the Anaconda 32 bit installed rather than 64 bit so things match up with ArcGIS. Once the file is copied and you have Anaconda 32 bit, import arcpy should work.