Installing ArcPy in Anaconda Environment – Step-by-Step Guide

anacondaarcgis-10.5arcpypython-2.7

I recently uninstalled Anaconda due to a conflict with my pandas packages. I use to have it configured with my ArcGIS Pro Python 3.65 environment.

Now I want to reinstall it, but for my ArcGIS Desktop 10.5/ Python 2.7 environment. I need to have an environment that has both the arcpy , pandas, and arcgis libraries/mods in it (which my current 2.7/ArcMap 10.5 does).

How can I install Anaconda for 2.7 without overwriting or interfering with my current ArcGIS 10.5/ Python 2.7 environment AND set up my arcpy/arcgis/pandas within that new Anaconda environment?

If I wanted to copy my current Arc – Python 2.7 env folder, where would I find it (ex: C:\Python27\ArcGIS10.5)?

Best Answer

I am going to show how you can replicate arcgis' python enviroment using conda and then plug arcpy to it. This enviroment shall be isolated from arcgis' python so whatever changes do to it, it shouldn't affect arcgis.


The first step is to create an enviroment to enable arcpy at. You can do this with anacoda by typing in the anaconda promt:

 conda create -n arcpy python=2.7

This will create a new enviroment within anaconda called arcpy.

Next you need the default packages the come with Arcgis' python. To find them open your arcgis program and in the python promt type:

 >>> from pip.operations import freeze
 >>> packages = freeze.freeze()
 >>>for p in packages:
         print(p)

  cycler==0.10.0
  functools32==3.2.3.post2
  future==0.15.2
  matplotlib==1.5.2
  ....

Save this to a file and make a note of its name as we are going to use it in the next step. For convienience here I am posting my output. I saved them to a file called arcgis_10.5_req.txt.

NB. please remove the version number for functools32. Also note that the default arcgis' numpy version is 1.9.3.. You might need to change toto 1.10 as conda finds 1.9 to be conflicting with scipy.

Next activate your conda enviroment and using condas' installer, install the before mentioned packages:

conda activate arcpy
conda install --file arcgis_10.5_req.txt

After the procedure completes, open your Arcgis' python directory and site-packages folder (..\Lib\site-packages) should be one *.pth file. Copy that file into your anacondas' arcpy enviroment site-packages folder.

in my machine it was :

C:\Python27\ArcGISx6410.5\Lib\site-packages\DTBGGP64.pth -> D:\Miniconda3\envs\arcpy\Lib\site-packages\DTBGGP64.pth

The *.pth file contains a number of extra folders for python to look for modules or objects.

Alternatively if don't want to copy the .pth file you can at the start of your script add the folders found in the .pth file to the sys.path manualy at each start.

>>> import sys
# or sys.path.append(r'C:\Program Files (x86)\ArcGIS\Desktop10.5\bin')
>>> sys.path.append(r'C:\Program Files (x86)\ArcGIS\Desktop10.5\bin64')
>>> sys.path.append(r'C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy')
>>> sys.path.append(r'C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcToolBox\Scripts')

Now you should be able to call arcpy from this env:

conda activate arcpy
python

>>> import arpcy
>>> shp = 'my_shapefile.shp'
>>> with arcpy.da.SearchCursor(shp,['OID@','SHAPE@AREA']):
     for row in cursor:
      print('Feature {} has an Area of {}').format(row[0],row[1])
Feature 0 has an area of 0.13687430446
....
Related Question