python - 打包一个要在控制台运行的Python项目 : pyproject. toml文件结构

标签 python command-line-interface setuptools pyproject.toml twine

我有一个 Python 项目的以下结构,我想将其打包以在控制台上有一个入口点。

example_prog
├── src
├   └── my_module_name
├       └── app.py
├       └── blueprint.py
├       └── cli.py
├       └── config.py
├       └── db.py
├       └── search.py
├       └── wsgi.py
├       └── static
├       └── templates
├── pyproject.toml
└── README.md

cli.py 程序包含一个 def main() 函数,其启动如下:

def main():
    parser = argparse.ArgumentParser(prog="my_module_name")

我已遵循此 link 中的说明并具有以下 pyproject.toml,其中应包含主文件的路径。

[project.scripts]
futdash = "futdash:cli:main"

但是当我使用 twine(在 testpypi 上)构建时,如下所示(这是我通常为其他包构建​​所做的事情):

python3 -m build
python3 -m twine upload --repository testpypi dist/*

但我收到以下错误:

* Creating venv isolated environment...
* Installing packages in isolated environment... (setuptools)
* Getting build dependencies for sdist...
configuration error: `project.scripts.my_module_name` must be python-entrypoint-reference
DESCRIPTION:
    Reference to a Python object. It is either in the form
    ``importable.module``, or ``importable.module:object.attr``.

GIVEN VALUE:
    "my_module_name:cli:main"

OFFENDING RULE: 'format'

DEFINITION:
    {
        "type": "string",
        "format": "python-entrypoint-reference",
        "$comment": "https://packaging.python.org/specifications/entry-points/"
    }

 ..
 ..

  File "/tmp/build-env-g403qutw/lib/python3.9/site-packages/setuptools/build_meta.py", line 341, in get_requires_for_build_sdist
    return self._get_build_requires(config_settings, requirements=[])
  File "/tmp/build-env-g403qutw/lib/python3.9/site-packages/setuptools/build_meta.py", line 320, in _get_build_requires
    self.run_setup()
  File "/tmp/build-env-g403qutw/lib/python3.9/site-packages/setuptools/build_meta.py", line 335, in run_setup
    exec(code, locals())
  File "<string>", line 1, in <module>
  File "/tmp/build-env-g403qutw/lib/python3.9/site-packages/setuptools/__init__.py", line 87, in setup
    return distutils.core.setup(**attrs)
  File "/tmp/build-env-g403qutw/lib/python3.9/site-packages/setuptools/_distutils/core.py", line 159, in setup
    dist.parse_config_files()
  File "/tmp/build-env-g403qutw/lib/python3.9/site-packages/setuptools/dist.py", line 867, in parse_config_files
    pyprojecttoml.apply_configuration(self, filename, ignore_option_errors)
  File "/tmp/build-env-g403qutw/lib/python3.9/site-packages/setuptools/config/pyprojecttoml.py", line 62, in apply_configuration
    config = read_configuration(filepath, True, ignore_option_errors, dist)
  File "/tmp/build-env-g403qutw/lib/python3.9/site-packages/setuptools/config/pyprojecttoml.py", line 126, in read_configuration
    validate(subset, filepath)
  File "/tmp/build-env-g403qutw/lib/python3.9/site-packages/setuptools/config/pyprojecttoml.py", line 51, in validate
    raise ValueError(f"{error}\n{summary}") from None
ValueError: invalid pyproject.toml config: `project.scripts.my_module_name`.
configuration error: `project.scripts.my_module_name` must be python-entrypoint-reference

ERROR Backend subprocess exited when trying to invoke get_requires_for_build_sdist

这是否是文件结构的问题,是否意味着更改[project.scripts]中的路径?

最佳答案

尝试更改第一个 :冒号到 .点。这应该适用于具有以下格式的结构:
<Project>.<File>:<Function>

来自:

[project.scripts]
futdash = "futdash:cli:main"

致:

[project.scripts]
futdash = "futdash.cli:main"

关于python - 打包一个要在控制台运行的Python项目 : pyproject. toml文件结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75120581/

相关文章:

python - 从python通过SSH连接Gitlab部署公钥

python - Unittest Django : Mock external API, 什么是正确的方法?

powershell - -s 在 powershell 中使用 Start-Job 时

ruby - 将 thor 用于复杂的命令行工具

node.js - NodeJS - 将命令行参数传递给所需的模块

python - Pyramid 中的 virtualenv + setuptools 问题

python - MT940格式解析器

python:如何根据值合并dict列表中的dict

python - 在 setup.py :install_requires, 中,什么时候应该使用 == 和 >=?

python - 使用生成步骤打包 Web 应用程序的 pythonic 方法是什么?