Import Python with various directory structures

I am working on a project where all the code in the source tree is divided into module directories, for example:

modules/check/lib/check.py
modules/edit/lib/edit.py

During installation, Python files are placed in the same directory program_nameunder Python site-packages. Therefore, all modules use syntax import program_name.edit.

Due to the directory structure and import, the source modules cannot import each other, so you will have to install them every time you want to run something in the source tree.

Therefore, my questions are: without changing the directory structure, how can I be sure that I modules/check/lib/check.pyimport from modules/edit/lib/edit.pyand what I site-packages/program_name/check.pyimport from site-packages/program_name/edit.py? And for a possible reorganization, what are the best practices for directory structure and import in such an environment?

+3
source share
2 answers

You can simply add directories /modules/to your PYTHONPATH in your dev environment. After installing packages into the site, a call import editinside check.py will import the correct module, since they are in the same directory. A call import editfrom your dev environment imports the one you added to your PYTHONPATH

+2
source

Why don't you set symlinks under prog_nameyour dev machine?

0
source

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


All Articles