How to properly structure internal scripts in a Python project?

后端 未结 7 1257
抹茶落季
抹茶落季 2021-02-14 02:58

Consider the following Python project skeleton:

proj/
├── foo
│   └── __init__.py
├── README.md
└── scripts
    └── run.py

In this case f

7条回答
  •  感动是毒
    2021-02-14 03:57

    If you like simplicity, and there are no additional restrictions on what you asked, add one __init__.py to the scripts folder, and to any other sibling folders, making them packages, then always use the absolute import form, as you said you do not want proj as a parent package of those and so there is no __init__.py there, and then call your scripts (instead) from inside the proj folder with:

    python -m scripts.run

    or whatever name you give to other scripts other than run.py

    This is similar to option 2 of @matejcik answer, but even simpler.

提交回复
热议问题