Custom Maya Startup: Adding User Python Site-Packages

 
 

Say you have an environment variable named PYROOT with a lis of a few paths where python libs are stored. Use python’s site module to add your lib directories as site directories like this:

import os
import site
PYTHONPATHS = []
if os.environ.has_key('PYROOT'):
	if os.pathsep in os.environ.get('PYROOT'):
		for p in str.split(os.environ.get('PYROOT'), os.pathsep):
			PYTHONPATHS.append(p)
	else:
		PYTHONPATHS.append(os.environ.get('PYROOT'))


if len(PYTHONPATHS):
	for j in PYTHONPATHS:
		site.addsitedir(j)

This quick script works cross-platform using os.pathsep to test for environment variable separators ‘;’ (win) and ‘:’ (linux) and using that to split the PYROOT string variable appending each of the paths to the PYTHONPATHS buffer list. By using the site.addsitedir module, python will treat these directories as normal site-package directories with all the built-in abilities to customize such as sitecustomize module and .pth files. If subdirectories are already organized into packages using __init__.py then they are already ready to be used and imported into Maya right away, or else if the directory has a .py file and no __init__.py just add an empty one.

To add your custom user site-packages directory just append the lines of code or create a new userSetup.py and make sure it is on your PYTHONPATH when you start up, previously explained here, or just put it in any one of the default maya scripts areas.