How to properly structure internal scripts in a Python project?

后端 未结 7 1254
抹茶落季
抹茶落季 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:40

    Solution

    There are multiple ways to achieve this. Both require creating a python package by adding a setup.py (building on @matejcik's answer).

    Option 1 (recommended): entry_point + console_scripts register a function in your project as the entry point to script execution (ie: proj:foo:cli:run).

    Option 2: scripts: Use this keyword argument in the setup() method to reference the path to your script (ie: `bin/script.py).

    Note

    I recommend using a CLI library/framework like Click so that your codebase is only concerned with maintaining application specific business logic rather than CLI robust framework feature logic. Also, click recommends using entry_point + console_scripts method of script integration due to cross-platform compatibility.

    Setup Tools - Automatic script creation: https://setuptools.readthedocs.io/en/latest/setuptools.html#automatic-script-creation

    Setup Tools - keyword arguments: https://setuptools.readthedocs.io/en/latest/setuptools.html#new-and-changed-setup-keywords

    Click GitHub: https://github.com/pallets/click/

    Click Setuptools integration: https://click.palletsprojects.com/en/master/setuptools/

提交回复
热议问题