views:

128

answers:

3

Hi,

I have written a small DB access module that is extensively reused in many programs.

My code is stored in a single directory tree /projects for backup and versioning reasons, and so the module should be placed within this directory tree, say at /projects/my_py_lib/dbconn.py.

I want to easily configure Python to automatically search for modules at the /projects/my_py_lib directory structure (of course, __init__.py should be placed within any subdirectory).

What's the best way to do this under Ubuntu?

Thanks,

Adam

+6  A: 

You can add a PYTHONPATH environment variable to your .bashrc file. eg.

export PYTHONPATH=/projects/my_py_lib
ar
+1 Simple, correct and works.
Adam Matan
A: 

Another option is to create a soft link in /usr/lib*/python*/site-packages/:

ln -s /projects/my_py_lib /usr/lib*/python*/site-packages/

That will make the project visible to all Python programs plus any changes will be visible immediately, too.

The main drawback is that you will eventually have *.pyc files owned by root or another user unless you make sure you compile the files yourself before you start python as another user.

Aaron Digulla
And you need root access.
jae
+2  A: 

on linux, this directory will be added to your sys.path automatically for pythonN.M

~/.local/lib/pythonN.M/site-packages/

So you can put your packages in there for each version of python you are using.

You need a copy for each version of python, otherwise the .pyc file will be recompiled every time you import the module with a different python version

This also allows fine grained control if the module only works for some of the versions of python you have installed

If you create this file

~/.local/lib/pythonN.M/site-packages/usercustomize.py

it will be imported each time you start the python interpreter

gnibbler
+1 nice, didn't know that one. Do you have a link to the documentation?
Aaron Digulla
This would make me put code outside my code tree.
Adam Matan
@Adam Matan, you can put symlinks to your code tree, but bear in mind that the .pyc files will get clobbered if you use different versions of Python, better to have a "release" script to copy it over there.
gnibbler
@Arron Digulia, the documentation is in `site.py` in the std lib :)
gnibbler