Select to view content in your preferred language

Using a user-specific Python startup script

3844
8
02-18-2016 10:00 AM
curtvprice
MVP Alum
2 8 3,844

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

try:
  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)
  else:
    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()
    sys.path.append(conda_site)
    ##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:
  print(msg)
  pass
Tags (2)
8 Comments
Luke_Pinner
MVP Regular Contributor

Is this saved as the special "usercustomize.py" module? 28.14. site — Site-specific configuration hook — Python 2.7.11 documentation

DanPatterson_Retired
MVP Emeritus

29.13. site — Site-specific configuration hook — Python 3.4.4 documentation

soon to be gone anyway

Deprecated since version 3.4: Support for the “site-python” directory will be removed in 3.5.

curtvprice
MVP Alum

Luke Pinner​ absolutely.

Dan Patterson​ you just had to spoil my day, didn't you.

I'm assuming there is a new way to do it I will have to figure out.

Fortunately, we are still at 3.4 with Pro so I don't need to rebuild this right away.

Luke_Pinner
MVP Regular Contributor

Dan Patterson, that deprecation is nothing to do with "site.USER_SITE/usercustomize.py".

It relates to:

$PREFIX/lib/site-python  (e.g. /usr/lib/site-python), which is added to sys.path in addition to the versioned site-packages. But only under Unix ("if os.sep == '/'")

Issue 19375: Deprecate site-python in site.py - Python tracker

Luke_Pinner
MVP Regular Contributor
DanPatterson_Retired
MVP Emeritus

29.13. site — Site-specific configuration hook — Python 3.5.1 documentation

so from their example in 3.5, it will look like this

Then the following version-specific directories are added to sys.path, in this order:

/usr/local/lib/pythonX.Y/site-packages/bar

/usr/local/lib/pythonX.Y/site-packages/foo

but only if you keep a /user/local/lib path, I presume.

curtvprice
MVP Alum

Dan, it looks like site-packages is still supported. It's site-python that's getting deprecated at 3.5.

This seems like a lot of hassle, but I think it's the best way to tweak the environment for different flavors of python and entry-point.

Luke_Pinner
MVP Regular Contributor

Dan Patterson wrote:

29.13. site — Site-specific configuration hook — Python 3.5.1 documentation

so from their example in 3.5, it will look like this

Then the following version-specific directories are added to sys.path, in this order:

/usr/local/lib/pythonX.Y/site-packages/bar

/usr/local/lib/pythonX.Y/site-packages/foo

but only if you keep a /user/local/lib path, I presume.

I'm not sure what you're trying to illustrate there Dan.  Other than the deprecation and removal of "site-python" (that nobody uses anyway), nothing has really changed in regards to system site-packages and per-user site-packages handling. /usr/local/lib/pythonX.Y is one of the system site-packages directories and is standard on Linux (for both Python 2 & 3). It's where stuff gets installed when you use pip/easy_install etc (without specifying the --user flag) rather than the distribution package manager (which installs to /usr/lib/pythonX.Y).

And more importantly...

Curtis' usercustomize.py will continue to work in 3.5

About the Author
PhD candidate (Geology), South Dakota Mines; Hydrologist, U.S. Geological Survey.
Labels