Python path: reusing the Python module

I wrote a small database access module that is reused in many programs.

My code is stored in a tree of one tree /projectsfor backup and version purposes, so the module should be placed in this directory tree, for example /projects/my_py_lib/dbconn.py.

I want to easily configure Python to automatically search for modules in a directory structure /projects/my_py_lib(of course, it __init__.pyshould be placed in any subdirectory).

What is the best way to do this on Ubuntu?

Thank,

Adam

+3
source share
3 answers

You can add an environment variable PYTHONPATHto your file .bashrc. eg.

export PYTHONPATH=/projects/my_py_lib
+8

linux, sys.path pythonN.M

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

, python.

python, .pyc , python

, python,

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

python

+2

- /usr/lib*/python*/site-packages/:

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

Python, .

, *.pyc, root , , , python .

0

Source: https://habr.com/ru/post/1734227/


All Articles