当我想从子包和python包中生成多个命令行应用程序时,如何编写准确的“setup.py”(entry_points)

2024-04-26 05:48:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想根据教程编写一个小型个人软件包:https://gehrcke.de/2014/02/distributing-a-python-command-line-application/。 目录的结构如下所示:

python-cmdline-bootstrap/
├── docs
├── test
├── bootstrap
│   ├── __init__.py
│   ├── __main__.py
│   ├── bootstrap.py
│   └── stuff.py
├── bootstrap-runner.py
├── LICENSE
├── MANIFEST.in
├── README.rst
└── setup.py

我进一步在引导折叠下添加了一个新模块(src)和脚本(case.py),如下所示:

python-cmdline-bootstrap/
...
├── bootstrap
│   ├── __init__.py
│   ├── __main__.py
│   ├── bootstrap.py
│   ├── stuff.py
│   ├── src
│       ├── __init__.py
│       ├── case.py
├── bootstrap-runner.py
...

{}的内容如下:

# -*- coding: utf-8 -*-

import argparse


def case():
    print("This a new command.")

我将以下行添加到setup.py中:

console_scripts = """
[console_scripts]
bootstrap = bootstrap.bootstrap:main
cccase = bootstrap.src.case:case
"""

安装后在终端中执行python setup.py install并运行cccase时,显示错误:

Traceback (most recent call last):
  File "/home/chxp/tmp/python3-test/bin/cccase", line 11, in <module>
    load_entry_point('cmdline-bootstrap==0.2.0', 'console_scripts', 'cccase')()
  File "/home/chxp/tmp/python3-test/lib/python3.8/site-packages/pkg_resources/__init__.py", line 489, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/home/chxp/tmp/python3-test/lib/python3.8/site-packages/pkg_resources/__init__.py", line 2852, in load_entry_point
    return ep.load()
  File "/home/chxp/tmp/python3-test/lib/python3.8/site-packages/pkg_resources/__init__.py", line 2443, in load
    return self.resolve()
  File "/home/chxp/tmp/python3-test/lib/python3.8/site-packages/pkg_resources/__init__.py", line 2449, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
ModuleNotFoundError: No module named 'bootstrap.src'

如果我使用./bootstrap-runner.pypython -m bootstrap,它可以正常工作,我认为它可能是setup.py中的错误。 因此,如何修改setup.py?我想在python包中生成不同的命令行应用程序

同时,如何在一次运行中使用./bootstrap-runner.pypython -m bootstrap测试多个命令行应用程序。 似乎我需要更改__main__.pybootstrap-runner.py中的内容来测试每个命令行应用程序,例如bootstrapcccase

原始脚本在“https://gitee.com/chxp/python-cmdline-bootstrap”上传,在^{下载

谢谢你的帮助

我在这里看到了解释:Why do I need to include sub-packages in setup.py。它完美地回答了我的问题


Tags: inpytesthomeinitsetuplineload
1条回答
网友
1楼 · 发布于 2024-04-26 05:48:27

我找到了解决办法!如果指定packages,函数setup中的packages参数似乎无法识别子模块

因此,以下更改将起作用:

from setuptools import setup, find_packages

setup(
    name="cmdline-bootstrap",
    packages=find_packages(),
    entry_points=console_scripts,
    version=version,
    description="Python command line application bare bones template.",
    long_description=long_descr,
    author="Jan-Philip Gehrcke",
    author_email="jgehrcke@googlemail.com",
    url="http://gehrcke.de/2014/02/distributing-a-python-command-line-application",
)

只需使用packages=find_packages()替换packages=["bootstrap"]

但是我仍然不知道如何真正解释这个问题,如果我仍然想使用^{,我需要做什么

相关问题 更多 >