Python:迁移 setup.py “scripts="到入口点 [英] Python: migrate setup.py "scripts=" to entry_points

查看:32
本文介绍了Python:迁移 setup.py “scripts="到入口点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用别人的python实用程序,foobartools,其原生环境是linux.Foobartools 是纯 python,所以没有理由不能在 Windows 上使用,我所在的位置.

I'd like to make use of someone else's python utility, foobartools, whose native environment is linux. Foobartools is pure python so there's no reason it can't be used on Windows, where I am.

在他们的 setup.py 中,他们使用旧样式 scripts=['bin/foobar'],.

In their setup.py they're using the older style scripts=['bin/foobar'],.

运行 pip install -eb:\code\foobar%pythonhome%\Scripts 中创建一个名为 foobar 的文件,但 Windows 没有即使脚本在 PATH 中,也不知道它.要使用它,我需要制作一个 @python %pythonhome%\scripts\foobar 批处理文件.这有效但不是最佳的(例如,ctrl-c 处理很丑陋).

Running pip install -e b:\code\foobar creates a file called foobar in %pythonhome%\Scripts, but Windows doesn't know about it even though Scripts is in PATH. To use it I need to make a @python %pythonhome%\scripts\foobar batch file. This works but is non-optimal (ctrl-c handling is ugly for instance).

我知道如果我添加较新的和推荐的 entry_pointssetup.py pip 的语法将在 Windows 上自动创建 Scripts\foobar.exe (我可以放弃批处理文件).在 Linux 上 Scripts\foobar 保持不变,每个人都很高兴.

I know that if I add the newer and recommended entry_points syntax to setup.py pip will automatically create Scripts\foobar.exe on Windows (and I can ditch the batch file). On Linux Scripts\foobar remains unchanged, and everybody is happy.

entry_points = {
    'console_scripts': ['foobar = foobartools:main'],
}

我不知道如何处理等式的右侧,foobartools:main.如何调用 bin/foobar?foobartools 模块没有一个单独的可调用函数或模块对应bin脚本,bin脚本相对复杂.

I don't know what to do with the right hand side of the equation, foobartools:main. How do make that call bin/foobar? The foobartools module doesn't have a single callable function or module like that corresponds to the bin script, and the bin script is relatively complicated.

我想对项目进行尽可能少的更改,以便 a) 提交的补丁有更好的机会被上游接受,并且 b) 如果上游仍然不感兴趣,那么我要做的工作就会减少来跟上它.

I want to make as few changes as possible to the project so that a) a patch submission has a better chance of getting accepted upstream, and b) if upstream remains uninterested there's less work for me to do to keep up with it.

[更新] 现有的源码树结构:

foobartools/
    bin/foobar
    foobartools/
        __init__.py
        foo/
            __init__.py
            one.py
            two.py
        bar/
            ...
        baz/
            ...
    setup.py

推荐答案

我得到了一个有效的 Scripts\foobar.exe,但它更改的代码比我希望的要多.我希望只对 setup.py 进行一些调整,或者至少在上游模块内部不会乱七八糟的东西.我不太了解它,无法确定我不会以某种方式挡路.

I got to a working Scripts\foobar.exe, but it changes more of the code than I wish. I was hoping to get by with just a couple of tweaks to setup.py, or at least something that doesn't mess around inside upstream modules. I don't know it well enough to be sure I'm not getting in the way somehow.

(1) 移动 bin/foobar --> foobartools/foobar_cli.py
(2) 在 setup.py 中,注释掉 #scripts=... 并添加:

(1) move bin/foobar --> foobartools/foobar_cli.py
(2) In setup.py, comment out #scripts=... and add:

entry_points = {
    'console_scripts': ['foobar = foobartools.foobar_cli:main'],
} 

[稍后] 对同一核心理念的改进,意味着我将我的胖脚排除在主要业务领域之外:

[later] A refinement of the same core idea, means I keep my fat feet out of the main business area:

(1) move ./bin/foobar --> ./foobar_cli/foobar.py (注意扩展名和文件夹变化)
(2) 添加一个空的 __init__.py 到同一个文件夹
(3) console_scripts': ['foobar = foobar_cli.foobar:main'],

(1) move ./bin/foobar --> ./foobar_cli/foobar.py (note extension as well as folder change)
(2) add an empty __init__.py to same folder
(3) console_scripts': ['foobar = foobar_cli.foorbar:main'],

这篇关于Python:迁移 setup.py “scripts="到入口点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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