3

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

asked Feb 24, 2010 at 11:03

3 Answers 3

8

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

export PYTHONPATH=/projects/my_py_lib
answered Feb 24, 2010 at 11:13
Sign up to request clarification or add additional context in comments.

Comments

2

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

answered Feb 24, 2010 at 12:25

3 Comments

+1 nice, didn't know that one. Do you have a link to the documentation?
@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.
@Arron Digulia, the documentation is in site.py in the std lib :)
0

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.

answered Feb 24, 2010 at 11:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.