linux下,如何把 python 的包安装成可执行脚本?

这个问题和 pyinstaller 一点关系都没有

比如 namekouvicorndjango-admin 这些东西,我们 pip install uvicorn 之后,就在命令行可以直接使用 uvicorn

╰─➤  uvicorn --version
Running uvicorn 0.17.4 with CPython 3.10.8 on Darwin

一些乱七八糟的信息如下:

╭─ponponon@MBP13ARM ~/Desktop/code/me/github/nameko  ‹master*› 
╰─➤  where uvicorn 
/Library/Frameworks/Python.framework/Versions/3.10/bin/uvicorn
/Library/Frameworks/Python.framework/Versions/3.10/bin/uvicorn

可以看到,这玩意就想一个 shell 脚本一样,指定了一下 python 解释器的路径

╭─ponponon@MBP13ARM ~/Desktop/code/me/github/nameko  ‹master*› 
╰─➤  cat /Library/Frameworks/Python.framework/Versions/3.10/bin/uvicorn                 
#!/Library/Frameworks/Python.framework/Versions/3.10/bin/python3.10
# -*- coding: utf-8 -*-
import re
import sys
from uvicorn.main import main
if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(main())

我现在也在写一个 cmd tool,希望可以实现 pip install xxx 之后,就可以在命令行使用 xxx

所以,我该怎么做?


我本以为和 setup.py 有关系,但是并没有发现什么端倪


seo 优化:

  • python怎么打包生成linux命令行可用软件?
  • python 写的工具如何在 linux 下调用?
  • dango-admin 是如何在命令行调用的?
  • flask 是如何在命令行调用的?
  • uvicorn、nameko 是如何在命令行调用的?
  • 使用Python编写命令行工具有什么好的库?
阅读 1.3k
1 个回答

已解决,和 console_scripts 有关系

The console_scripts Entry Point

The second approach is called an ‘entry point’. Setuptools allows modules to register entrypoints which other packages can hook into to provide certain functionality. It also provides a few itself, including the console_scripts entry point.

This allows Python functions (not scripts!) to be directly registered as command-line accessible tools.

具体可看:python-packaging console-scripts


以 nameko 举例:

setup.py 的 entry_points 的 console_scripts 就是干这个事情的

entry_points={
    'console_scripts': [
        'nameko=nameko.cli.main:main',
    ],
    'pytest11': [
        'pytest_nameko=nameko.testing.pytest'
    ]
}

链接:https://github.com/nameko/nam...


以 django 举例:

setup.cfg 的 options.entry_point 的 console_scripts 就是干这个事情的

[options.entry_points]
console_scripts =
    django-admin = django.core.management:execute_from_command_line

链接:https://github.com/django/dja...


flask、celery、glances 等等同理

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题