开发者

Python path: Reusing Python module

开发者 https://www.devze.com 2022-12-20 22:23 出处:网络
I have written a small DB access module that is extensively reu开发者_开发技巧sed in many programs.

I have written a small DB access module that is extensively reu开发者_开发技巧sed 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


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

export PYTHONPATH=/projects/my_py_lib


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消