Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I see a lot of threads asking how you can create a separate Python installation (e.g. the Anaconda or Python XY distribution) that can use arcpy, while not 'breaking' the original ArcGIS python installation.

I had this same problem, and I think I have found a 'universal' solution. So far we have done this on about 8 computers at work and it all works fine.

So the question: How to install a separate Python distribution that can use ArcGIS?

share|improve this question

2 Answers 2

EDIT: 2015 Solution

My solution now is to use a small module which goes off and hunts for ArcGIS on your PC. Once find it adds the correct paths to the environment so that you can import arcpy. The usage goes like this:

try:
    import archook #The module which locates arcgis
    archook.get_arcpy()
    import arcpy
except ImportError:
    # do whatever you do if arcpy isnt there.

The module is this:

'''
Locate ArcPy and add it to the path
Created on 13 Feb 2015
@author: Jamesramm
'''
import _winreg
import sys
from os import path
def locate_arcgis():
  '''
  Find the path to the ArcGIS Desktop installation.

  Keys to check:

  HLKM/SOFTWARE/ESRI/ArcGIS 'RealVersion' - will give the version, then we can use
  that to go to
  HKLM/SOFTWARE/ESRI/DesktopXX.X 'InstallDir'. Where XX.X is the version

  We may need to check HKLM/SOFTWARE/Wow6432Node/ESRI instead
  '''
  try:
    key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
                          'SOFTWARE\\Wow6432Node\\ESRI\\ArcGIS', 0)

    version = _winreg.QueryValueEx(key, "RealVersion")[0][:4]

    key_string = "SOFTWARE\\Wow6432Node\\ESRI\\Desktop{0}".format(version)
    desktop_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
                                  key_string, 0)

    install_dir = _winreg.QueryValueEx(desktop_key, "InstallDir")[0]
    return install_dir
  except WindowsError:
    raise ImportError("Could not locate the ArcGIS directory on this machine")

def get_arcpy():  
  '''
  Allows arcpy to imported on 'unmanaged' python installations (i.e. python installations
  arcgis is not aware of).
  Gets the location of arcpy and related libs and adds it to sys.path
  '''
  install_dir = locate_arcgis()  
  arcpy = path.join(install_dir, "arcpy")

  # Set the 'binary' directory according to the bitness of our interpreter:  
  if sys.maxsize > 2**32:
    bin_dir = path.join(install_dir, "bin64")
  else:  
    bin_dir = path.join(install_dir, "bin")

  scripts = path.join(install_dir, "ArcToolbox", "Scripts")  
  sys.path.extend([arcpy, bin_dir, scripts])

It is very simple and I'm sure can be made more robust, but it does the job. I plan to make it available on pypi..

It has a few advantages over the more 'manual' original method:

  1. You dont need to configure each python environment separately...just import the module and off you go
  2. It does not edit/create any registry keys or permanent environment variables, so there is no chance of messing up any existing python/arcgis configuration.
  3. You dont need to know anything about paths to various folders/files, or even what version of arcgis you have installed. The module will find it all for you.

I have left the original answer below, but this new solution is (IMO) 100% better.


2014 answer

Ok, so this answer works for the Anaconda 64bit Python distribution with ArcGIS 10.1 64bit on Windows 7/Windows Server. Some of (or all) of the tips given below may apply to any other windows distribution of python.

First, install anaconda, it should go to the directory C:\Anaconda. Check the box 'make system default python'. It may give a warning that there is another python installed, but continue. Then:

Setup Paths and Environment Variables

First thing to do is to copy over the DTBGGP64.pth file to the new distribution directory (C:\Anaconda) from C:\Python27\ArcGISx6410.1\Lib\site-packages. The naming is arbritrary, but must have the .pth extension This will allow you to import ArcPy when using the new distribution.

Note: The correct location of the DTBGGP64.pth file should be in the site-packages directory (e.g. C:\Anaconda\Libs\site-packages). I have found that with some older versions of Anaconda, it works if sat in the top level directory

Next, the environment variables must be checked: In the system variable Path, the existing path to the python directory should be replaced with the new path. (e.g. C:\Anaconda;C:\Anaconda\Scripts;) This will tell windows where the default python directory is.

Make sure the user variable PYTHONPATH is also correct. This should point to any libraries you wish to use which are not described by .pth files or are installed in site-packages. I.E this might be your own development packages, or packages such as mapnik which have a different installation location.

Check Registry Settings

On some machines, the above may be enough to ensure that you can use arcpy from the new python and that python can be used within ArcGIS. On other machines, you may need to check registry keys.

The following registry keys have python settings:

HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.7\PythonPath
There should be one key and its’ value should be C:\Anaconda\Lib;C:\Anaconda\DLLs (Or the corresponding folders for your python installation)

HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.7\InstallPath
The key should be C:\Anaconda

You may have other python versions installed...in which case change the version number in the above paths

HKEY_CLASSES_ROOT\Python.CompiledFile\DefaultIcon This is the location of the .ico file to use for .pyc files

HKEY_CLASSES_ROOT\Python.CompiledFile\shell\open\command This is the command to issue when opening a .pyc file from the shell. You should change the python.exe path to the location of your desired python version

HKEY_CLASSES_ROOT\Python.File The keys here are the same as those for Python.CompiledFile except that they apply to a .py file. Again, make the relevant changes. It may have an extra key for 'Edit with IDLE' or 'Edit with Pythonwin'. Again, make the relevant changes so that the paths in these keys point to your desired python version.

HKEY_CLASSES_ROOT\Python.NoConFile These are the same as for Python.File, but should point to the pythonw.exe program where appropriate.

HKEY_LOCAL_MACHINE\SOFTWARE\ESRI\Python10.1
The PythonDir key points to the python installation to use within ArcMap etc.. You may wish for this to remain as the python installation as installed by ESRI, or point it to your new installation. It may require other changes if you point ArcMap to the new python installation. There may be issues with the versions of packages (e.g. numpy) installed by the new distribution. I have not encountered any.

For the majority of my work, this is irrelevant as I typically want to use python on it's own and occasionally have access to arcpy. I rarely wish to use python from within arcmap..Therefore I have done little testing of this final step, but it seems to work for everything I have tried so far.

I imagine that the process would be similar for installing Python(x,y) etc.

Other notes

The contents of the .pth file is a list of paths pointing to the following folders in your ArcGIS installation: bin64, arcpy, ArcToolbox\Scripts

For example, my .pth file contains the following:

C:\Program Files (x86)\ArcGIS\Desktop10.2\bin64
C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy
C:\Program Files (x86)\ArcGIS\Desktop10.2\ArcToolbox\Scripts
share|improve this answer
2  
You should also mark this as the accepted answer. –  Chad Cooper Feb 18 '14 at 13:17
    
Can't do that until 2 days have passed since posting :) –  James Feb 18 '14 at 13:20
    
hmm...when I click the tick, I get a message saying 'you can accept your own answer in 2 days'... –  James Feb 18 '14 at 13:28
2  
Don't forget that ArcGIS (10.2 and 10.1) has very specific version requirements for numpy - must be 1.6.1 or you can break arcpy. –  Luke Feb 18 '14 at 23:22
1  
A really nice feature of anaconda is you can set it up with virtual environments with the versions you need for ArcGIS or any other package. For example, you could have a virtual environment set up to work with ArcGIS that has the versions that Esri tools are expecting, and another one that doesn't include arcpy when working totally outside of arcgis. –  Curtis Price Feb 13 at 18:31

My solution is to set up a startup script that sets of the path based on the python environment you are using. This method has the (huge) advantage that you don't need admin access to write .pth files in python installations. This script is setup to use both 32 bit and 64 bit Anaconda and ArcGIS/arcpy.

    # Startup script to link Anaconda python environment with ArcGIS
    #
    # 1. Install Anaconda, setup environment to match your ArcGIS version
    # 2. Edit the paths below
    # 3. Put this startup script in the startup folder as "usercustomize.py"
    #    Startup folder can be found with: "C:\Python27\ArcGIS10.2\python -m site --user-site"
    #    Usually will be:
    # C:\Users\%USERNAME%\AppData\Roaming\Python\Python27\site-packages

    import sys
    import os

    # edit these paths to match your setup
    arcver = "10.2"
    # Anaconda home folders
    conda32 = r"D:\Users\cprice\Anaconda"
    conda64 = r"D:\Users\cprice\Anaconda64"
    # here are the conda environments you've set up use with ArcGIS
    # arc1022 is the environment setup for ArcGIS
    conda_env32 = "{}/envs/{}".format(conda32, "arc1022")
    conda_env64 = "{}/envs/{}".format(conda64, "arc1022")

    # do not edit below this line

    # ArcGIS Python home folders
    # i.e. C:\Python27\ArcGIS10.2
    arcver = arcver[:4]
    arcpy32 = r"C:\Python27\ArcGIS{}".format(arcver)
    arcpy64 = r"C:\Python27\ArcGISx64{}".format(arcver)

    try:
        if sys.version.find("64 bit") < 0:
            conda_path = os.path.normpath(conda_env32)
            arcpy_path = os.path.normpath(arcpy32)
            arcpy_pthfile = os.path.normpath(
                arcpy_path + "/lib/site-packages/desktop{}.pth".format(arcver))
        else:
            conda_path = os.path.normpath(conda_env64)
            arcpy_path = os.path.normpath(arcpy64)
            arcpy_pthfile = os.path.normpath(
                arcpy_path + "/lib/site-packages/DTBGGP64.pth")

        for p in [conda_path, arcpy_path, arcpy_pthfile]:
            if not os.path.exists(p):
                raise Exception("{} not found".format(p))

        ## print(sys.prefix)
        ## print(conda_path)

        # If running ArcGIS's Python, add conda modules to path
        if (sys.executable.lower().find("desktop" + arcver) != -1
            or sys.prefix.lower().find("arcgis10") != -1):
            sys.path.append(os.path.dirname(arcpy_path))
            conda_site = os.path.join(conda_path, "lib", "site-packages")
            if not os.path.isdir(conda_site):
                raise Exception()
            sys.path.append(conda_site)
            print("usercustomize.py: added conda paths to arc")

        # if running Anaconda add arcpy to path
        elif sys.prefix.lower() == conda_path.lower():
            with open(arcpy_pthfile, "r") as f:
                sys.path +=  [p.strip() for p in f.readlines()]
            print("usercustomize.py: added arcpy paths to conda")

    except Exception as msg:
        print(msg)
        pass
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.