Making separate Python installation that can call ArcPy?
I created a small (and relatively popular) 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 hosted on github here: https://github.com/JamesRamm/archook
It is very simple and I'm sure can be made more robust, but it does the job.
It is available on pypi: pip install archook
It has a few advantages over the more 'manual' original method (detailed below):
- You dont need to configure each python environment separately...just import the module and off you go
- 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.
- 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.
- No admin authentication needed
- It is not specific with anaconda - it will work with any python installation
I have left the original answer below, but this 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
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
In case anyone is trying to do this using ArcGIS Pro (i.e. with python 3 instead of python 2):
I use spyder within the anaconda package. The way I got this to work was by going to Tools > Python Interpreter in Spyder, then pointing Spyder to the propy.bat file currently located at C:\Program Files\ArcGIS\Pro\bin\Python\Scripts\propy.bat.
This activates the correct python version in Spyder and allows you to import arcpy.