如何为网络 python 包安装注册入口点? [英] How to register Entry Points for network python package installs?

查看:63
本文介绍了如何为网络 python 包安装注册入口点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户环境,其中大多数 python 包都安装在网络共享上,并通过 PYTHONPATH 环境变量提供.Python 本身仍然安装在本地.其中一些包需要注册 setuptools 入口点.通常,这会通过运行包的 setup.py 文件来实现,但由于这些文件安装到网络位置,setup.py 永远不会与本地安装python.

I have a user environment where most python packages are installed on a network share and are made available via the PYTHONPATH environment variable. Python itself is still installed locally. Some of those packages need to register setuptools Entry Points. Normally, this would happen by running the setup.py file for the package, but because these are installed to a network location, that setup.py is never run with the local installation of python.

有没有办法在本地python安装中不用安装包就可以注册入口点?

Is there a way to register Entry Points without installing a package in the local python installation?

为插件系统注册回调的入口点示例

An example of an entrypoint that registers a callback for a plugin system

setup(
    ...
    entry_points={
        'xx_plugin': ['value = package.module:func'],
    }
) 

推荐答案

运行 setup.py

console_scriptsgui_scripts 入口点创建包装可调用对象的独立脚本.coverage 包就是一个很好的例子.为方便起见,它有一个入口点来创建包装器脚本.创建控制台脚本的示例 setup.py 片段:

Run setup.py

The console_scripts and gui_scripts entry points create standalone scripts that wrap a callable object. The coverage package is a good example of this. It has an entry point to create a wrapper script for convenience. Example setup.py snippet to create a console script:

setup(
    ...
    entry_points = {
        'console_scripts': [
            'script_name = package.module:function'
        ]
    }
)

可以通过 setup.py 仅将脚本安装到指定目录.除非传递了 --force 参数,否则不会覆盖现有脚本.

It's possible to only install the scripts to a specified directory via setup.py. An existing script will not be overwritten unless the --force argument is passed.

python setup.py install_scripts --install-dir /some/alternate/location

对于插件,入口点作为元数据存储在通常与包一起安装的鸡蛋中.示例插件入口点:

For a plugin, the entry point is stored as metadata in an egg that's typically installed with the package. Example plugin entry point:

setup(
    ...
    entry_points = {
        'group_name': [
            'plugin_name = package.module:function'
        ]
    }
)

可以通过 setup.py 仅将包的元数据(包括任何入口点)安装到指定目录.

It's possible to only install the metadata of a package (including any entry points) to a specified directory via setup.py.

python setup.py egg_info --egg-base $PYTHONPATH

自己动手

如果setup.py 无法运行来写入egg 元数据,完全可以自己编写,因为它只是一个目录中的文本文件.基于上述入口点的元数据示例:

Roll your own

If setup.py can't be run to write the egg metadata, it's totally possible to roll your own, as it's only a text file in a directory. Example metadata based on the above entry point:

# $PYTHONPATH/entry_points.egg-info/entry_points.txt
[group_name]
plugin_name = package.module:function

要测试入口点查找:

import pkg_resources as pkg

for entry_point in pkg.iter_entry_points('group_name'):
    plugin_name = entry_point.name
    plugin_module_name = entry_point.module_name
    plugin_callable = entry_point.load()
    # do something with plugin

这篇关于如何为网络 python 包安装注册入口点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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