organizing many python scripts, in a large corporate environment.

eryksun () eryksun at gmail.com
Mon Mar 14 09:45:51 EDT 2011


On Monday, March 14, 2011 2:38:50 AM UTC-4, bukzor wrote:

> I've written this many times. It has issues. In fact, I've created a
> library for this purpose, for the following reasons.

If you're linking to a common file, couldn't you just add in the base folder there? I don't think it's a bad practice to hard-code an absolute path in a single file. If the path changes you only have to update one line. For example:

# script.py
import _path   # _path.py is a symbolic link

# _path.py:
base = '/absolute/path/to/base'
import site
site.addsitedir(base) 

This also adds paths from any .pth files located in the base folder, but that shouldn't be necessary if all of the scripts are located within one package and sub-packages. 

On a Posix-compliant system, you should be able to use the following to avoid hard-coding the path (untested, though):

# _path.py:
import os.path
import site
base = os.path.dirname(os.path.realpath(__file__))
site.addsitedir(base) 

Possibly it has to be abspath(realpath(__file__)), but the docs say realpath returns a 'canonical format', which should be the absolute path. I can't check right now, and this doesn't work for me at all on Windows. Python can't resolve the symbolic links created by mklink to the real path, which is odd since mklink has been standard in Windows for many years now. Maybe Python 3.x handles it better.



More information about the Python-list mailing list