Using a user-specific Python startup script

Blog Post created by curtvprice Champion on Feb 18, 2016

The following script gives you the ability to fully customize your python environment.


The example below is set up for ArcGIS 10.2 with ArcGIS background GP and  anaconda 32 and anaconda 64 installed.


You can tweak this to your hearts content to adapt to all your favorite Python environments!


# 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
#    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

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

import sys
import os

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

  # If running ArcGIS python, add conda modules to path
  if sys.prefix.lower().find("arcgis10") != -1:
    conda_site = os.path.join(conda_path, "lib", "site-packages")
    if not os.path.isdir(conda_site):
      raise Exception()
    ##print("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("added arcpy paths to conda")

except Exception as msg: