Sunday 1 October 2017

Making separate Python installation that can call ArcPy?


I see a lot of Q&As 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.


How to install a separate Python distribution that can use ArcGIS?



Answer



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):



  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.

  4. No admin authentication needed

  5. 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

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...