【问题标题】:How can I create an empty python package?如何创建一个空的 python 包?
【发布时间】:2020-04-30 17:38:33
【问题描述】:

我想使用诗歌工具创建一个空的(元)包,主要是为了简化将依赖项列表组合在一起的过程。如果我按如下方式创建我的 project.toml:

[build-system]
requires = ["poetry"]

[tool.poetry]
name = "metapackage"
version = "1.0.0"
description = "My empty metapackage"
authors = ["Me"]
license = "MIT"

[tool.poetry.dependencies]
numpy = "*"

然后执行poetry build,报错:

$ mkdir -p metapackage
$ python -m poetry build --no-interaction --format wheel -vvvUsing virtualenv: /Users/duncan/opt/miniconda3/envs/py37
Building metapackage (1.0.0)

[ValueError]
metapackage is not a package.

Traceback (most recent call last):
  File "/Users/duncan/opt/miniconda3/envs/py37/lib/python3.7/site-packages/clikit/console_application.py", line 131, in run
    status_code = command.handle(parsed_args, io)
  File "/Users/duncan/opt/miniconda3/envs/py37/lib/python3.7/site-packages/clikit/api/command/command.py", line 120, in handle
    status_code = self._do_handle(args, io)
  File "/Users/duncan/opt/miniconda3/envs/py37/lib/python3.7/site-packages/clikit/api/command/command.py", line 171, in _do_handle
    return getattr(handler, handler_method)(args, io, self)
  File "/Users/duncan/opt/miniconda3/envs/py37/lib/python3.7/site-packages/cleo/commands/command.py", line 92, in wrap_handle
    return self.handle()
  File "/Users/duncan/opt/miniconda3/envs/py37/lib/python3.7/site-packages/poetry/console/commands/build.py", line 30, in handle
    builder.build(fmt)
  File "/Users/duncan/opt/miniconda3/envs/py37/lib/python3.7/site-packages/poetry/masonry/builder.py", line 19, in build
    builder = self._FORMATS[fmt](self._poetry, self._env, self._io)
  File "/Users/duncan/opt/miniconda3/envs/py37/lib/python3.7/site-packages/poetry/masonry/builders/wheel.py", line 44, in __init__
    super(WheelBuilder, self).__init__(poetry, env, io)
  File "/Users/duncan/opt/miniconda3/envs/py37/lib/python3.7/site-packages/poetry/masonry/builders/builder.py", line 68, in __init__
    includes=self._package.include,
  File "/Users/duncan/opt/miniconda3/envs/py37/lib/python3.7/site-packages/poetry/masonry/utils/module.py", line 72, in __init__
    source=package.get('from'),
  File "/Users/duncan/opt/miniconda3/envs/py37/lib/python3.7/site-packages/poetry/masonry/utils/package_include.py", line 15, in __init__
    self.check_elements()
  File "/Users/duncan/opt/miniconda3/envs/py37/lib/python3.7/site-packages/poetry/masonry/utils/package_include.py", line 61, in check_elements
    raise ValueError('{} is not a package.'.format(root.name))

我是否遗漏了一些东西来告诉诗歌这个包裹没有内容?

附录:我很乐意使用诗歌来创建一个填充有元数据的.dist-info 目录,以便pip 看到已安装的元数据包,如果有更好的工具,我很乐意切换。

【问题讨论】:

  • 您的目录中有 init.py 文件吗?这就是 python 要求它被视为一个包的条件。
  • @Jonhasacat,不,这个包的文件数正好为零。
  • 我相信这对于 setuptools 是可行的(也许其他人也是如此),以防你不依赖于 poetry
  • @sinoroc,我没有绑定到poetry,您能否将解决方案作为单独的答案发布?
  • @Duncan 我相信这只是将packagespy_modules 排除在外的问题。您是否已经熟悉 setuptools 或根本不熟悉?

标签: python python-packaging python-poetry


【解决方案1】:

使用 setuptools 可能如下所示:

pyproject.toml

[build-system]
build-backend = "setuptools.build_meta"
requires = ["setuptools"]

setup.cfg

[metadata]
name = metapackage
version = 1.0.0
description = My empty metapackage
authors = Me
license = MIT

[options]
install_requires =
    numpy

注意options packages, py_modules, scripts, and ext_modules 是如何未被填充的。

【讨论】:

    【解决方案2】:

    使用诗歌构建的包的绝对最低要求是它包含可导入的 Python 模块或包。

    因此,您至少需要一个允许声明import metapackage 的文件,仅一个没有任何文件的文件夹是不够的:

    选项 1,带模块

    .
    ├── pyproject.toml
    └── metapackage.py  # can be empty
    

    选项 2,带包

    .
    ├── pyproject.toml
    └── metapackage
        └── __init__.py  # can be empty
    

    【讨论】:

    • 我相信对于 poetry 可能是这样,但对于 setuptools 则不然。
    • @sinoroc 我将这些信息添加到我的帖子中,感谢您指出。我不知道其他构建工具可以跳过该步骤。
    猜你喜欢
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-23
    • 1970-01-01
    相关资源
    最近更新 更多