【问题标题】:python pip install wheel with custom entry points带有自定义入口点的 python pip install wheel
【发布时间】:2014-11-19 17:18:53
【问题描述】:

在 Windows 上的 python virtualenv 中,我安装了一个自定义包,它覆盖 setuptools.command.install.easy_install.get_script_args 以创建新的自定义类型的入口点“custom_entry”

我有另一个包,我想用 setuptools 准备暴露一个自定义入口点。

如果我准备了这个包的 egg 发行版并使用我修改过的 easy_install.exe 安装它,这将正确创建自定义入口点。

但是,如果我准备一个 wheel 分发版并使用修改后的 pip.exe 安装它,则不会添加自定义入口点。

为什么 pip 不遵循与 easy_install 相同的安装过程?

阅读 pip 的源代码,似乎wheel.py 中的函数 get_entrypoints 排除了除 console_scripts 和 gui_scripts 之外的所有入口点。这是正确的吗?

如果是这样,我应该如何为 pip 安装安装自定义入口点?

---- 编辑

看来我应该提供更多细节。

在我的第一个包custom-installer 中,我在custom_install.__init__.py 中覆盖(真的是猴子补丁)easy_install.get_script_args:

from setuptools.command import easy_install

_GET_SCRIPT_ARGS = easy_install.get_script_args

def get_script_args(dist, executable, wininst):

    for script_arg in _GET_SCRIPT_ARGS(dist, executable, wininst):
        yield script_arg # replicate existing behaviour, handles console_scripts and other entry points

    for group in ['custom_entry']:
        for name, _ in dist.get_entry_map(group).items():
            script_text = (
                ## some custom stuff
            )
            ## do something else
            yield (## yield some other stuff) # to create adjunct files to the -custom.py script
            yield (name + '-custom.py', script_text, 't')

easy_install.get_script_args = get_script_args
main = easy_install.main

在那个包的 setup.py 中,我为我的自定义安装程序提供了一个 (console_script) 入口点:

entry_points={
    'console_scripts': [
        'custom_install = custom_install.__init__:main'
    ]
}

使用 pip 正确安装此软件包会创建安装程序脚本 /venv/Scripts/custom_install.exe

使用我的第二个包customized,我可以从setup.py 安装常规和自定义入口点,用于两个模块custom 和console。

entry_points={
    'console_scripts': [
        'console = console.__main__:main'
    ],
    'custom_entry': [
        'custom = custom.__main__:main'
    ]
}

无论安装过程如何,我都希望看到这两个入口点都已安装。

如果我将包 customized 构建为 egg 发行版,并使用 custom-installer 创建的 custom_install.exe 安装它,那么 customized 的两个入口点都会安装。

我希望能够使用 pip 将此包安装为轮文件,但从阅读源代码来看,pip 似乎明确跳过了“console_scripts”和“gui_scripts”以外的任何入口点:

def get_entrypoints(filename):
    if not os.path.exists(filename):
        return {}, {}
    # This is done because you can pass a string to entry_points wrappers which
    # means that they may or may not be valid INI files. The attempt here is to
    # strip leading and trailing whitespace in order to make them valid INI
    # files.
    with open(filename) as fp:
        data = StringIO()
        for line in fp:
            data.write(line.strip())
            data.write("\n")
        data.seek(0)
    cp = configparser.RawConfigParser()
    cp.readfp(data)
    console = {}
    gui = {}
    if cp.has_section('console_scripts'):
        console = dict(cp.items('console_scripts'))
    if cp.has_section('gui_scripts'):
        gui = dict(cp.items('gui_scripts'))
    return console, gui

随后,pip 使用与 easy_install 完全不同的一组代码生成入口点脚本。据推测,我可以覆盖这些 pip 的实现,就像使用 easy_install 所做的那样,来创建我的自定义入口点,但我觉得我走错了路。

谁能建议一种更简单的方法来实现与 pip 兼容的自定义入口点?如果没有,我可以覆盖get_entrypoints 和move_wheel_files。

【问题讨论】:

  • 好的,根据接受的答案here,这种配置似乎不包含在设计中的点轮部署中。 《不要将包安装和系统部署混为一谈》
  • 也许答案是提供一个控制台脚本入口点,它将运行我想要的自定义入口点的其余配置。
  • 阅读您的更新后,听起来您可能需要编写一些自定义或修补 pip 本身的内容。我认为 pip 有一个邮件列表。您可能想在那里交叉发布。
  • 通过阅读,我认为 pip 根本不适合用于安装后的配置设置。我找不到应该使用什么 python 工具,所以我推出了自己的解决方案。这是一团糟,但它有效。

标签: python windows pip entry-point python-wheel


【解决方案1】:

您可能需要在setup.py 文件中使用关键字console_scripts。请参阅以下答案:

它基本上表明您需要在setup.py 脚本中执行以下操作:

entry_points = {
       'console_scripts': ['custom_entry_point = mypackage.mymod.test:foo']
       }

另见:http://calvinx.com/2012/09/09/python-packaging-define-an-entry-point-for-console-commands/

【讨论】:

  • 嗨。感谢您的回复。我想我一定没有说清楚。我的入口点的配置方式与“console_scripts”不同。我有一些我想做的自定义设置。
猜你喜欢
  • 2019-01-10
  • 1970-01-01
  • 2015-09-14
  • 2018-12-13
  • 2019-06-06
  • 2016-04-15
  • 2013-10-04
  • 2021-06-03
  • 1970-01-01
相关资源
最近更新 更多