Python设置,将一个模块安装为另一个模块的子模块? [英] Python setup, install one module as a sub module of another module?

查看:170
本文介绍了Python设置,将一个模块安装为另一个模块的子模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个软件包作为扩展安装到另一个python软件包的子模块中。

I would like to create a package that installs into a submodule of another python package as an extension.

基本上,原始模块的设置如下:

Basically the original module is set up like so:

origmodule/
    __init__.py
    stuff.py
    ext/
        __init__.py
        # This module is empty

然后将扩展模块安装到空的origmodule中.ext模块。阅读 distutils 的说明尚不清楚是否可行或受支持。最终目标是使我的扩展模块一旦安装就可以像这样导入:

And then have my extension module installed into the empty origmodule.ext module. Reading the instructions for distutils it was not clear if this is possible or supported. The end goal is so that my extension module once installed would be imported like this:

import origmodule.ext.my_extension

推荐答案

您无法使用setuptools进行此操作,支持在另一个软件包中进行安装。

You cannot do that with setuptools, installing inside another package is not supported.

相反,您想使用进入点。您的 origmodule 应该查找注册为特定键的入口点的任何内容,而您的扩展模块应该为以下内容进行注册:

Instead, you want to use entry points instead. Your origmodule should look for anything registered as an entry point for a specific key, and your extension module should register itself for that key.

您的扩展程序注册了扩展点:

Your extension registers an extension point:

entry_points={
    'some.opaque.string.unique.to.origmodule':
        ['my_extension = my.extension.package:some.entry.point', ]
}

您的 origmodule 然后可以通过询问 pkg_resources

which your origmodule then can discover by asking pkg_resources:

import pkg_resources

for entrypoint in pkg_resources.iter_entry_points(group='some.opaque.string.unique.to.origmodule'):
    extension = entrypoint.load()

Babel项目为例。 Babel可以从文件中提取可翻译文本;它知道如何为Python源代码执行此操作,但它还支持可以从其他格式提取此类文本的扩展。

Take a look at the Babel project as an example. Babel can extract translatable text from files; it knows how to do this for Python source code, but it also supports extensions that can extract such text from other formats.

此类提取方法可以注册为扩展点。 Babel在编写提取方法部分中对此进行了记录。然后,邮件提取代码在提取文本时会加载这些入口点

Such extraction methods can be registered as extension points. Babel documents this in the Writing Extraction Methods section. The message extraction code then loads those entry points when extracting text:

GROUP_NAME = 'babel.extractors'

# ...

for entry_point in working_set.iter_entry_points(GROUP_NAME,
                                                 method):
    func = entry_point.load(require=True)
    break

Mako模板语言提供了这样的插件;它注册入口点,该入口点指向实际实现

The Mako templating language supplies such a plugin; it registers the entry point, which points to the actual implementation:

[babel.extractors]
mako = mako.ext.babelplugin:extract





def extract(fileobj, keywords, comment_tags, options):
    # ...

这篇关于Python设置,将一个模块安装为另一个模块的子模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆