Python: migrate setup.py “scripts=” to entry_points

情到浓时终转凉″ 提交于 2020-01-12 14:07:02

问题


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.

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

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).

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'],
}

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.

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.

[update] The existing source tree structure:

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

回答1:


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) 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 (note extension as well as folder change)
(2) add an empty __init__.py to same folder
(3) console_scripts': ['foobar = foobar_cli.foorbar:main'],



来源:https://stackoverflow.com/questions/30518806/python-migrate-setup-py-scripts-to-entry-points

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!