Главная » Error » How To Package And Deploy CLI Applications With Python PyPA Setuptools Build

How To Package And Deploy CLI Applications With Python PyPA Setuptools Build

В этой статье

By yaythomas on 30 August 2021

This article covers how to package your Python code as a CLI application using just the official PyPA provided tools, without installing additional external dependencies.

If you prefer reading code to reading words, you can find the full example demo code discussed in this article here: example repo of Python CLI packaged with PyPA setuptools build

Run your Python code from the command line

Run a Python file as a script

Since Python is a scripting language, you can easily run your Python code from the CLI with the Python interpreter, like this:

# run a python source file as a script
$ python mycode.py

# run a python module
$ python -m mycode

Create a CLI shortcut to bootstrap your Python application

If you want to run your Python script as a CLI application with a user-friendly name and not have to type in the Python interpreter & path in front of it, you could of course just create an executable shortcut file in your /bin directory like this:


python3 /path/to/mycode.py “$@”

The “$@” passes all the CLI arguments from your shortcut launcher to your Python script.

But this is not all that useful when you actually want to distribute your code, because you’d still have to create & permission this executable file on all your end-users’ machines somehow, in addition to provisioning the actual Python dependencies and your app itself.

Thankfully, Python has great well-tested & widely used built-in mechanisms for doing exactly this for you – so no, you don’t even need to jerry-rig your own shortcut like this at all!


Poetry — кроссплатформенный инструмент. Теоретически он должен одинаково работать в Linux, MacOS и Windows. Системные требования предполагают использование актуальных версий Python, поэтому начиная с версии Poetry 1.2 не поддерживаются Python 2.7 и 3.5, будьте внимательны!

Poetry создает изолированное виртуальное окружение для вашего проекта, инсталлируя все необходимое и разруливая конфликты зависимостей (если они в принципе решаемы).

По умолчанию установка происходит в домашний каталог пользователя, но это можно изменить путем переопределения переменной окружения POETRY_HOME:


Установка на OSX и Linux

Этапы установки на Mac и Linux идентичны:

1 Проверить версию Python (в консоли python –version, и если версия Python по умолчанию 2.7, проверить python3 –version).

2 Чтобы установить актуальную версию вводим в консоль:

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python –

Если дефолтная версия языка не устраивает или если для установки хочется воспользоваться версией питона 3.x, а по умолчанию стоит 2.7, используйте команду:

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3 –

Обратите внимание: процесс установки может занять довольно длительное время.

Каталог bin, в который происходит установка инструмента, в Unix расположен в $HOME/.poetry/bin.

Установка на Windows

Для инсталляции Poetry:

1Откройте Powershell (Win + X далее i, или найдите PoweShell через поиск).

2 Введите команду:

(Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python –

Перезапустите оболочку и все заработает. Папку установки для Windows вы найдете в %USERPROFILE%.poetrybin.

Для корректной работы установщик должен иметь возможность найти исполняемые файлы py.exe -3 или py.exe -2.

Установка через PIP и PIPX

Для данного варианта установки необходимо иметь на борту менеджер пакетов PIP (или PIPX).

Для установки Poetry через PIP выполните команду:

pip install –user poetry

Имейте ввиду, что зависимости пакета Poetry могут вызвать конфликт с уже установленными пакетами в вашей системе.

Команды Pipx для установки, обновления и удаления инструмента:

pipx install poetry
pipx upgrade poetry
pipx uninstall poetry

Pipx поддерживает только версии Python старше 3.6.

NoneType: ‘NoneType’ object is not subscriptable. Как быть, где допустил ошибку?

  • 1 подписчик
  • 2 часа назад
  • 44 просмотра


  • windows
    • Windows

    • +1 ещё


  • Как сделать повторение кода Python в exe?

    • 1 подписчик
    • 14 часов назад
    • 64 просмотра


  • python
    • Python

    • +1 ещё


  • How to package your Python code as a CLI application the proper way

    The standard way to package your Python code is to use setuptools. You use setuptools to create distributions that you can install with pip.

    setuptools has been around for ages, and is currently (August 2021) in a bit of a transitional phase. This has been the case for a few years. This means that there are different ways of achieving the same thing using this tool-set, as the new and improved ways slowly have been supplanting the old:

    • setup.py – the old way
    • setup.cfg – the sort-of newer
    • pyproject.toml (aka PEP 517 & PEP 518) – shiny & new

    The key to creating your own CLI application is to specify an entry_point in either your setup.cfg or setup.py file.

    The pyproject.toml specification does define this property (as [project.scripts]), but the standard PyPA build has not yet implemented actually doing anything with this property yet.

    Should you use setup.cfg, setup.py or pyproject.toml to configure Python packaging?

    The short answer is: for the moment, you probably should have all three.

    Now for the longer answer. You don’t necessarily have to have all three, but if you don’t you need to be sure you know exactly what you’re doing and why, otherwise you’re setting yourself up for mysterious errors down the line. If you’re not interested in the evolution & background of these mechanisms, feel free to skip to the next section.

    In the beginning was setup.py

    setup.py is the older, traditional way of packaging Python projects. Since setup.py is literally a Python script in itself, it is very powerful because you can script whatever advanced installation functionality you want as part of the install.

    But just because you can, doesn’t mean you should. The more unusual scripting you do as part of your install, the more your install becomes brittle & unpredictable on diverse client machines where you don’t necessarily have strict control over the state & configuration of those machines.

    Evolution to setup.cfg

    By comparison, setup.cfg is a config file, not an installation script like setup.py. setup.cfg is static, setup.py is dynamic.

    setup.cfg lets you specify declarative config – meaning that you can define your project meta-data without having to worry about scripting. This is a good thing because you avoid having to run arbitrary code during installs, which will make your security & ops teams happy, and you don’t have to maintain boilerplate code in your source. Bonus!

    Although it has been there alongside setup.py since the beginning, setup.cfg has taken more of a central role over the years. You can more or less accomplish the same thing with either, so from this perspective it doesn’t really matter which you use.

    However, even if you do ALL your configuration in setup.cfg you do still need a stub setup.py file unless you are running a PEP517 build. We’ll discuss this new build system in the next section.

    Enter pyproject.toml

    pyproject.toml is the official, anointed successor to setup.py and setup.cfg, but it has not reached feature parity with its predecessors yet. This new file format has come as a result of the PEP517 build specification.

    One of the notable features of the new Python build mechanisms specified in PEP517 is that you don’t have to use the setuptools build system – other build & packaging tools like Poetry and Flit can use the same pyproject.toml specification file (PEP621) to package python projects.

    Eventually all these tools should be using the exact same pyproject.toml file format, but be aware that historically build tools other than setuptools have had their own ways of specifying CLI entry-points, so be sure to check the documentation for whichever tool you end up using to double-check that it’s conforming to the latest PEP621 standard. Here, we are just going to focus on how to do this with setuptools.

    While the latest version of the pyproject.toml specification did add definitions for project meta-data that you will usually find in setup.cfg and/or setup.py, the setuptools build tool does NOT yet support using the meta-data from pyproject.toml. Other PEP517 compliant tools like Flit & Poetry do support projects with only a pyproject.toml file, so if you use those you don’t need setup.py and/or setup.cfg.

    You can find the full file format specification for pyproject.toml in PEP621.

    For all the gory details & progress of implementing full support for pyproject.toml metadata in setuptools, you can track the discussion here: https://github.com/pypa/setuptools/issues/1688

    Recommended Python packaging setup in 2021

    If you are using PyPA’s setuptools during this transitional phase of Python packaging, while you can get away with using one or the other combination of setup.py, setup.cfg & pyproject.toml to specify your meta-data and build attributes, you probably want to cover your bases and avoid subtle problems by having all 3 as follows:

    1. have a minimal pyproject.toml to specify the build system
    2. put all project related config in setup.cfg
    3. have a simple shim setup.py

    By “subtle problems” I mean inconsistencies like editable installs not working or builds that look like they’re working but they’re not actually using the meta-data you thought you specified (which you might only discover at deployment, urk!). So let’s avoid the unpleasantness!

    In this setup, since pyproject.toml and setup.py are only minimalist shims, your individual project related configuration is only contained in the one place in setup.cfg. Therefore you’re not needlessly duplicating values between different files.


    [project]name=”infer_pyproject”version=”0.1.0″description=”Create a pyproject.toml file for an existing project.”authors=[{name=”Martin Thoma”, email=”[email protected]”},{email=”[email protected]”}]license={file = “LICENSE.txt”}readme=”README.md”requires-python=”>=3.6″keywords=[“packaging”, “dependency”, “infer”, “pyproject.toml”]classifiers=[“Topic :: Software Development”]# Requirements: This is done differently by poetry!dependencies=[“Click>=7.0”][project.optional-dependencies]dev=[“black>=18.3-alpha.0”,][project.urls]homepage=”https://github.com/MartinThoma/infer_pyproject”documentation=”https://github.com/MartinThoma/infer_pyproject”repository=”https://github.com/MartinThoma/infer_pyproject”[project.scripts]poetry=”infer_pyproject.cli:main”[build-system]requires=[“setuptools >=35.0.2”,”setuptools_scm >=2.0.0, <3"]build-backend="setuptools.build_meta"[tool.black]line-length=88target_version=['py36']include='.pyi?$'exclude='''(/(.eggs # exclude a few common directories in the| .git # root of the project| .hg| .mypy_cache| .tox| .venv| _build| buck-out| build| dist)/| foo.py # also separately exclude a file named foo.py in# the root of the project)'''

    Poetry и виртуальное окружение

    По умолчанию, Poetry автоматически создает виртуальное окружение для вашего проекта в cache-директории:

    • для Windows — C:UsersAppDataLocalpypoetryCachevirtualenvs;
    • для Linux — ~/.cache/pypoetry/virtualenvs;
    • для macOS — ~/Library/Caches/pypoetry/virtualenvs.

    В эту директорию будут сохраняться все зависимости для проекта.

    Подробнее о том, что такое виртуальное окружение и как оно работает:Виртуальное окружение (venv)
    Виртуальное окружение Python (venv)

    Рассылка в телеграм боте?

    • 1 подписчик
    • 4 часа назад
    • 72 просмотра


  • windows
    • Windows


  • Как получить response?

    • 1 подписчик
    • 15 часов назад
    • 92 просмотра


  • Показать ещёЗагружается…
  • Вакансии с Хабр Карьеры

    Системный администратор Windows

    от 1 400 €

    Системный инженер Windows

    Золотое Яблоко
    от 100 000 ₽

    Инженер технической поддержки в группу эксплуатации промышленных сервисов

    от 120 000 до 150 000 ₽
    Ещё вакансии

    Заказы с Хабр Фриланса

    Разработка задания (node.js)

    18 окт. 2022, в 15:46
    10 руб./за проект

    Сделать Webview Android из веб приложения

    18 окт. 2022, в 15:23
    5000 руб./за проект

    Решить задачу на голом Питоне

    18 окт. 2022, в 15:19
    2500 руб./за проект
    Ещё заказы

    Минуточку внимания

    Присоединяйтесь к сообществу, чтобы узнавать новое и делиться знаниями


    Самое интересное за 24 часа

    • Можно ли заменить в ноутбуке экран на другой, с более высоким разрешением?

      • 2 подписчика
      • 1 ответ
    • Как вывести общее количество товаров магазина Woocommerce в записи/на странице с помощью шорткода?

      • 2 подписчика
      • 0 ответов
    • Можно ли удалить рекламу Yandex с сайта?

      • 4 подписчика
      • 3 ответа
    • Как исправить эту ошибку docker-compose?

      • 1 подписчик
      • 1 ответ
    • Как скачивать с защищенных каналов Telegram?

      • 8 подписчиков
      • 1 ответ
    • Какие есть сервисы кэширования для сайта?

      • 7 подписчиков
      • 1 ответ
    • Как Отправить сгенерированный пдф файл на электронную почту?

      • 2 подписчика
      • 1 ответ
    • Как дать роли права на чтение только со slave?

      • 2 подписчика
      • 1 ответ
    • Как изменить код таким образом, чтобы промисы выполнялись поочередно?

      • 2 подписчика
      • 1 ответ
    • Как сгрупировать значения multiselect инпута в подмассивы если в форме несколько multiselect инпутов с одним названием tags[]?

      • 2 подписчика
      • 1 ответ
    • © Habr
    • О сервисе
    • Обратная связь
    • Блог

    Как завершить выполнение кода принудительно?

    • 1 подписчик
    • 13 часов назад
    • 103 просмотра


  • python
    • Python


  • Сборка и настройка пакета

    Чтобы собрать пакет, запускаем команду build.

    poetry build

    Building project-poetry (0.1.0)
    – Building sdist
    – Built project-poetry-0.1.0.tar.gz
    – Building wheel
    – Built project_poetry-0.1.0-py3-none-any.whl

    В результате в папке dist будут сформированы пакеты.

    Результат сборки пакета находится “dist”

    Если мы хотим ограничить сборку определенным типом, можно просто использовать параметр -F.

    poetry build -F wheel # or sdist

    Чтобы развернуть пакет, нужно настроить репозитории и учетные данные. Для этого применяется команда config.

    poetry config repositories.priv https://my.repository

    В примере происходит настройка репозитория с именем priv и Url-адресом https://my.repository.

    Сохранить учетные данные для этого репозитория также можно с помощью команды config:

    poetry config http-basic.priv user password

    В приведенной команде user и password соответствуют имени пользователя и паролю для входа в наш частный репозиторий. Обратите внимание, если не указать пароль, то его будет предложено ввести при развертывании пакета.

    Закончив настройку, мы можем просто развернуть пакет следующей командой:

    poetry publish

    Если использован частный репозиторий, следует указать его имя Priv с помощью опции -r.

    Конфигурация Poetry

    Конфигурация Poetry настраивается с помощью команды poetry config. После первого запуска команды, Poetry создаст файл конфигурации config.toml, в котором будут храниться все заданные параметры. Находится файл в директории:

    • для Windows — C:UsersAppDataRoamingpypoetry;
    • для Linux — ~/.config/pypoetry;
    • для macOS — ~/Library/Application Support/pypoetry.

    Для примера, попробуем настроить Poetry так, чтобы он создавал виртуальное окружение virtualenvs не в cache-директории, а в корневом каталоге проекта. Для этого установим переменной virtualenvs.in-project значение true:

    poetry config virtualenvs.in-project true

    Чтобы убедиться, что значение успешно установилась, выполним команду:

    poetry config virtualenvs.in-project

    Чтобы посмотреть все текущие параметры Poetry, используйте команду poetry config –list.

    Теперь все зависимости будут устанавливаться в папку .venv в корне вашего проекта.

    Если параметр нужно удалить, используйте флаг –unset:

    poetry config virtualenvs.in-project –unset

    Подробнее о настройке конфигурации Poetry читайте тут.

    Create CLI entry point configuration for your Python project

    Sample project structure

    Let’s work through an example of a simple CLI application.

    The project structure looks like this:

    │ my-repo/
    │- mypackage/
    │- mymodule.py
    │- pyproject.toml
    │- setup.cfg
    │- setup.py


    This is just some arbitrary code that we want to call directly from the CLI:

    def my_function():
    print(‘hello from my_function’)

    def another_function():
    print(‘hello from another_function’)

    if __name__ == “__main__”:
    “””This runs when you execute ‘$ python3 mypackage/mymodule.py'”””


    To allow editable installs (useful for your local dev machine) you need a shim setup.py file.

    All you need in this file is this bit of boilerplate:

    from setuptools import setup


    You could actually skip the setup.cfg file and set your properties in setup() itself in setup.py, but this will make your migration harder in the future when the new PEP517 build system, like a death-star, is fully operational. I mention this because you’ll see a lot of examples on Stack Overflow & friends that go this way – it is not wrong, per se, but be aware that it is the older way of doing things.

    An old-style setup.py file would look something like this:

    from setuptools import setup

    # To provide executable scripts, use entry points in preference to the
    # “scripts” keyword. Entry points provide cross-platform support and allow
    # pip to create the appropriate form of executable for the target platform.
    ‘console_scripts’: [
    ] },


    The setup.cfg file is where the real magic happens. This is where you set your project-specific properties.

    [metadata] name = mypackage
    version = 0.0.1

    [options] packages = mypackage

    [options.entry_points] console_scripts =
    my-application = mypackage.mymodule:my_function
    another-application = mypackage.mymodule:another_function

    • name
      • The build system uses this value to generate the build output files.
      • If you do not specify this, your output filename will have “UNKNOWN” instead of a more user-friendly name.
    • version
      • The build system uses this value to add a version number to your output files.
      • If you do not specify this, your output filename will contain “0.0.0”.
    • packages
      • Use this property to tell the build system which packages to build.
      • This is a list, so you can specify more than one package.
      • If you’re not sure what a “package” is in Python, just think of it as the name of the directory your code lives in.
      • ❗If you do not specify this, your build output will not actually contain your code. If you forget to specify this, your package & deploy will look like it’s working, but it won’t actually package the code you want to run and it will not actually deploy correctly.
    • console_scripts
      • This property tells the build system to create a shortcut CLI wrapper script to run a Python function.
      • This is a list, so you can create more than one CLI application from the same code-base.
      • In this example, we are creating two CLI shortcuts:
        • my-application, which calls my_function in mypackage/mymodule.py.
        • another-application, which calls another_function in mypackage/mymodule.py.
      • The syntax for an entry is: = [


      • The name on the left will become the name of your CLI application. This is what an end-user will type in the CLI to invoke your application.
      • If you do not specify this property, your build will not create any CLI shortcuts for your code.
      • ❗Remember that you have to include the root package of the code you reference here under options.packages, otherwise the build tool will not actually package the code you’re referencing here!

    There are many more meta-data properties that you can (and maybe should!) specify in setup.cfg – here is a more comprehensive setup.cfg example. Given here instead is the bare minimum for a tidy build & packaging experience.

    Of the additional unlisted properties, of especial interest is install_requires, with which you specify dependencies – in other words, any external packages that your code depends on and that you want the installer to install alongside your application.

    [options] install_requires =
    importlib; python_version == “2.6”


    All you need in your minimalist pyproject.toml file is:

    [build-system] build-backend = “setuptools.build_meta”
    requires = [“setuptools”, “wheel”]

    In the pyproject.toml specification, project.scripts is the equivalent to console_scripts in setup.py and setup.cfg. However, at present this functionality is not implemented yet by the setuptools build system.

    Слетел пароль и много перестало работать, в чём проблемы?

    • 1 подписчик
    • 4 часа назад
    • 56 просмотров


  • python
    • Python


  • Как отремаппить клавиатуру целиком?

    • 1 подписчик
    • 3 часа назад
    • 39 просмотров


  • python
    • Python

    • +2 ещё


  • Use python -m build to create a python distribution

    build, aka PyPA build, is the more modern PEP517 equivalent of the older setup.py sdist bdist_wheel build command with which you might be familiar.

    If you’ve not done this before, you can install the build tool like this:

    $ pip install build

    Now, in the root of your project directory, you can run:

    $ python -m build

    This will result in two output files in the dist directory:

    • dist/mypackage-0.0.1.tar.gz
    • dist/mypackage-0.0.1-py3-none-any.whl

    The tool will create the ./dist directory for you if it doesn’t exist already.

    What this command does is to create a source distribution tarball (the tar.gz file), and then also create a wheel from that source distribution. A wheel (.whl) is a versioned distribution format that deploys faster because during installation you can skip the build step necessary for source distributions, and there are better caching mechanisms for it.

    The output filenames you see here follow a defined format that you can find specified in the PEP427 wheel file name convention.

    You’ll notice that the build tool uses name and version from setup.cfg to generate these filenames – which is why, even though you strictly speaking don’t need to specify these properties, they are useful if you want nicely named & easily identifiable outputs.

    Install your wheel with pip

    You can use pip to install the distribution you just created. (I’m sure pip doesn’t need any introduction to any Pythonista…)

    $ pip install dist/mypackage-0.0.1-py3-none-any.whl

    How PyPA build creates CLI shortcuts

    The pip install command will install your package and create the CLI shortcuts (the ones you specified in setup.cfg) in the current Python environment’s bin directory.

    • {Python Path}/bin/my-application
    • {Python Path}/bin/another-application

    Under the hood, these shortcut files are actually just a more sophisticated version of the quick-and-dirty bash file we created in the beginning. The auto-generated my-application shortcut file in the bin/ directory looks like this:

    # -*- coding: utf-8 -*-
    import re
    import sys
    from mypackage.mymodule import my_function
    if __name__ == ‘__main__’:
    sys.argv[0] = re.sub(r'(-script.pyw|.exe)?$’, ”, sys.argv[0])

    Testing your install in a clean environment

    If you want to test whether your shiny new package is installable, create a fresh new virtual environment and install your package into it so that you can test it in isolation.

    # create virtual environment
    $ python3 -m venv .env/fresh-install-test

    # activate your virtual environment
    $ . .env/fresh-install-test/bin/activate

    # install your package into this fresh environment
    $ pip install dist/mypackage-0.0.0-py3-none-any.whl

    # your shortcuts are now in the venv bin directory
    $ ls .env/fresh-install-test/bin/

    # so you can run it directly from the cli
    $ my-application
    hello from my_function

    # and run the second application
    $ another-application
    hello from another_function

    Как перейти по ярлыку?

    • 1 подписчик
    • 9 часов назад
    • 52 просмотра


  • python
    • Python


  • Publishing & distributing your Python package

    Publishing means how you make your Python package available to your end-users.

    How you publish your package depends on your deployment plan for your specific requirements. A full discussion of these is beyond the scope of this article, but just to get you started, some of the options are:

    • You can publish to and use pip to install from a private git repository.
    • You can create your own private Python repository manager.
    • You could just use pip to install the whl or sdist from a file-share in your organization.
    • If you are planning to release your application publicly to the official PyPI repository, you can use twine to upload the distribution to PyPi.
      • Be aware that you very probably should be a lot more detailed in filling in your project’s meta-data than the deliberately bare-bones minimal example given here if you are planning to create a public package.
    • Whereas pip installs to whichever Python environment is active at the time, this can get messy on end-user machines that you do not control – for example, shared dependencies can clash with other applications’ requirements.
      • If you want to install your application into an isolated environment, purposely separate just for your app with the dependencies for your app isolated from and not polluting the main system-wide Python installation, you can use pipx to install from a git repo (such as a private repo in your organization) or even just a file-path.
    • You can email your wheels around as attachments and tell people to install. Just kidding, just kidding! Don’t do this – just because it’s been known to happen doesn’t make it right. . .


    • 1990-08-09: First commit of cpython
    • 1998: distutils was drafted in IPC7 (source)
    • 1999-03-22: First distutil commit to cpython
    • 2000-11-16: PEP 229 — Using Distutils to Build Python
    • 2003-07-06: First capture of pypi.org found on waybackmachine
    • 2004-03-19: First commit of setuptools
    • 2006-05-13: First version of setuptools was released
    • 2007-09-14: First commit of virtualenv
    • 2008-10-16: First commit of pip
    • 2013-08-13: First commit of twine
    • 2015-01-24: First commit of PyPI warehouse
    • 2015-03-16: First version of flit was released
    • 2015-09-30: PEP 517 — A build-system independent format for source trees (provisional)
    • 2015-10-26: PEP 516 — Build system abstraction for pip/conda etc (rejected)
    • 2015-11-11: PEP 508 — Dependency specification for Python Software Packages (active)
    • 2016-05-10: PEP 518 — Specifying Minimum Build System Requirements for Python Projects (provisional)
    • 2016-11-18: First commit of pipfile
    • 2017-01-20: First commit of pipenv
    • 2018-02-28: First version of Poetry was released
    • 2020-06-22: PEP 621 – still not supported by poetry in August 2022 (#3332)

    Как ускорить разворачивание принтеров с принт-сервера на терминалы?

    • 1 подписчик
    • час назад
    • 18 просмотров


  • python
    • Python


  • See also

    • Jeff Triplett: Why Python devs should use Pipenv, 2018-02-28.
    • Sarahan, McCormick, Fillion-Robin: The Sheer Joy of Packaging, 2018.
    • Poetry: The pyproject.toml file
    • Frost Ming: A deeper look into Pipenv and Poetry, 2019-01-04.
    • Chad Smith: Five Myths About Pipenv, 2018-11-30.

    Please enable JavaScript to view the comments powered by Disqus.


    Jul 30, 2019
    by Martin Thoma




    • 2packaging
    • 141Python

    How to structure a Python CLI project

    For the sake of clarity, this example just directly calls a simple Python function from the CLI. Your code is very likely to be more involved.

    How best to structure your code in any given application is, of course, a very. . . debatable. . . topic . So instead of making bold claims about what is “best”, lets instead just look at what a typical tidy structure might look like… which is to say, while this is a relatively common way of doing things, it’s not necessarily THE way.

    │ my-repo/
    │- mypackage/
    │- mynamespace/
    │- anothermodule.py
    │- anothernamespace/
    │- arbmodule.py
    │- mymodule.py
    │- cli.py
    │- pyproject.toml
    │- setup.cfg
    │- setup.py

    If you create your entry-point function as def main() in cli.py then your setup.cfg file entry_points configuration simply becomes:

    [options.entry_points] console_scripts =
    my-application = mypackage.cli:main

    You can think of your functional code as a library, and the CLI is effectively a client or consumer of that library. Break your code into namespaces and modules that make sense for you – you can group together code by functional area, or by dependency, or by object, or by whatever categorization scheme works for you.

    If you think of the CLI as a consumer of your library’s API, it makes sense to encapsulate the code specific to CLI handling in its own module. You can name this what you like, but cli.py does have the benefit of being snappy. In this module you will very probably import something like argparse, to parse your CLI input arguments, print out errors when someone invokes your CLI with the wrong arguments, assign defaults and generate help & usage messages.

    Here is a real-life example of a large project structured like this, with a CLI handling module that encapsulates all CLI functionality and invokes the underlying program being called like you would an API.

    Список команд

    Poetry охватывает множество функций, связанных с управлением конфигурацией и публикацией пакетов:

    • shell — при первом запуске этой команды в каталоге вашего проекта Poetry создает виртуальную среду Python, которая будет связана с проектом. Вместо создания папки, содержащей библиотеки зависимостей, Poetry создает среду на глобальном системном пути. Как только эта виртуальная среда будет создана, ее можно активировать в любое время, запустив poetry shell в каталоге проекта.
    • install— устанавливает зависимости, указанные в pyproject.toml. При первой установке создается файл .lock, который содержит фактические номера версий каждого установленного пакета. Номера версий в .lock приоритетнее, чем находящиеся в pyproject.toml.
    • update— если для пакетов в pyproject.toml существуют более новые версии, они будут установлены, и .lock файл будет обновлен.
    • add— добавление зависимости к pyproject.toml. Пакет устанавливается сразу после применения команды.
    • remove— удаление библиотеки зависимостей.
    • config— пути к текущей виртуальной среде или ее переменные. Передача опции –list вернет текущие значения конфигурации.
    • check— проверяет pyproject.toml на наличие ошибок.
    • show— возвращает вывод всех пакетов, установленных в проекте, включая зависимости зависимостей.
    • run— выполняет сценарий, заданный в разделе tool.poetry.scripts в pyproject.toml.
    • build— собирает исходники и пакеты в архив.
    • publish— публикует вывод предыдущей сборки во внешнем репозитории, например, в PyPi.

    Чтобы получить справку из командной строки с полным списком команд, просто вызовите poetry.

    Команда “poetry” выведет весь список доступных команд


    Python PackageA bundle of software. This includes code and meta-data, such as requirements, a short and a long description, the license. It also contains instructions how to build the software. Formerly this was done with distutils.distutilsLets you create source distributions (python setup.py sdist). Sometime building takes a long time, so you might want to share already built distributions. You can do that with python setup.py bdist.setuptoolsLike distutils, but a 3rd party library. It is de-facto standard, but does not come with Python.setup.pydependency declarationPython file which specifies a package. As it can be arbitrary code, there is no way to know the dependencies of a package for sure without executing setup.py.PyPI (Python Packaging Index) software repositoryPyPI is the official third-party software repository for Python. Here people can share their code in form of Python packages.easy_installEasy_install is a package manager which is replaced by pip, because it could not uninstall and did not know what was installed. Other reasons as well.egg distribution distribution format”Egg” is a single-file importable distribution format for Python-related projects. Eggs are to Pythons as Jars are to Java, but eggs are richer than jars; they hold interesting metadata such as licensing details, release dependencies, … (source). It is a zip file.pippip is a de facto standard package manager for Python. It allows to install packages and installs required packages. pip introduced requirements.txtrequirements.txtdependency declarationIt allows pinning versions of a dependency. The setup.py includes abstract requirements, the requirements.txt includes concrete ones. Abstract requirements are more flexible, concrete ones are stable.wheel distribution distribution formatThe wheel binary package format is specified in PEP 427. It is similar to egg distributions. It is a zip file.twineAllowed to securely upload a package to PyPI.condaconda is a Python-agnostic packaging tool and installer. If you need more than Python / if you don’t have Python installed. It supports C, Fortran, R, Perl, Java, …pipfile and pipfile.lock dependency declarationA pipfile should be easier to maintain than multiple requirements.txt files, e.g. for testing / environments (dev, stage, prod). The pipfile.lock is automatically generated and tracks interdependencies of the required packages; it is similar to gemfile.lock in Ruby and yarn.lock in JavaScript. pipenv is the tool to use with those files. SO-Questions: 28 for pipfile, 485 for pipenv pyproject.tomldependency declarationA build-system independent way to specify project dependencies. setup.py is bad as it is 3rd party and requires code execution. It is a way to step away from distutils / setuptools. The specified build system can be setuptools as well. The pyproject.toml is similar to the Cargo.toml of Rust pyenv Install different Python versions. It is similar to rbenv from Ruby.pipenvWrapper for virtualenv. Has 485 questions on SO. See pipfile for more info. virtualenvwrapper Another virtuelenv wrapper. Has 570 questions on SO. Poetry Meant to be a successor of pipenv, but seems not production-ready yet (source, 13 SO questions). DepHell A Python project management tool. Can convert between setup.py <-> pyproject.toml. flit Flit is a way to put Python packages and modules on PyPI. It is a 3rd party replacement for setuptools, but has no SO tag.

    Как создать разные сессии/ прокси для каждого запроса?

    • 1 подписчик
    • 18 минут назад
    • 12 просмотров


  • windows
    • Windows

    • +2 ещё


  • Источники

    • https://pybit.es/articles/how-to-package-and-deploy-cli-apps/
    • https://pythonchik.ru/okruzhenie-i-pakety/menedzher-zavisimostey-poetry-polnyy-obzor-ot-ustanovki-do-nastroyki
    • https://qna.habr.com/q/1080866
    • https://martin-thoma.com/pyproject-toml/
    Решите Вашу проблему!