V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
scriptB0y
V2EX  ›  分享创造

将 Python 项目打包成一个 binary,即使没有 Python 解释器也能跑

  •  
  •   scriptB0y ·
    laixintao · 2020-03-01 22:08:48 +08:00 · 4510 次点击
    这是一个创建于 1514 天前的主题,其中的信息可能已经有所发展或是发生改变。

    打包的细节可以看这个 PR: https://github.com/laixintao/iredis/pull/279

    非常简单,用 PyOxidizer 打包之后自动上传到 github 的 release,只要写好这个 Github Action 之后,以后只要在 master 打 tag 就会自动 release。

    缺点就是 PyOxidizer 打包,如果第三方依赖(几乎是必然)有 __file__ 的依赖的话,就无法打包成一个文件,必须带一个 lib/ 目录。

    有兴趣的可以在 release 页面下载 tar 解压之后运行一下。

    github: https://github.com/laixintao/iredis

    14 条回复    2020-03-11 00:08:49 +08:00
    jakezh
        1
    jakezh  
       2020-03-02 05:28:04 +08:00 via iPhone
    pyinstaller 了解一下
    scriptB0y
        2
    scriptB0y  
    OP
       2020-03-02 08:52:40 +08:00
    @jakezh pyinstaller 本质上是吧整个库的文件都打包起来,运行的时候再解压到文件系统,比 PyOxidizer 要慢很多
    jakezh
        3
    jakezh  
       2020-03-02 09:08:44 +08:00
    去 github 看了一下还真是, 居然还是用 rust 写的, 等有空试试
    scriptB0y
        4
    scriptB0y  
    OP
       2020-03-02 10:06:37 +08:00
    @jakezh 是的,本质山使用 Cargo 那套 build 系统,直接把解释器和 py 代码嵌套一起去了。

    还有一个 Nutika 是直接翻译 Py 代码到 C,然后编译个 bianry 的。
    PyOxidizer 这里有个对比各个类似打包工具的文档:

    https://pyoxidizer.readthedocs.io/en/stable/comparisons.html
    dragonszy
        5
    dragonszy  
       2020-03-02 10:11:04 +08:00
    战略马克,的确 pyinstaller 太慢了。
    bbxiong
        6
    bbxiong  
       2020-03-02 15:24:55 +08:00
    马克 py 打包
    lc1450
        7
    lc1450  
       2020-03-02 18:32:34 +08:00
    没有编译好的版本吗,折腾了半天,也没有编译出来 rust 太难了
    scriptB0y
        8
    scriptB0y  
    OP
       2020-03-02 19:01:31 +08:00
    @lc1450 大哥。。心疼你。。。release 页面有编译好的啊,下载解压就能运行了……

    https://github.com/laixintao/iredis/releases/latest

    帖子里说了……

    > 有兴趣的可以在 release 页面下载 tar 解压之后运行一下。
    lc1450
        9
    lc1450  
       2020-03-02 19:58:06 +08:00
    @scriptB0y 我说的是 PyOxidizer
    scriptB0y
        10
    scriptB0y  
    OP
       2020-03-02 20:17:09 +08:00
    @lc1450 PyOxidizer 用 cargo install 就可以了吧
    lc1450
        11
    lc1450  
       2020-03-02 20:32:52 +08:00
    是呀就是这么弄的,然后下载了一大堆文件编译,然后就各种报错
    scriptB0y
        12
    scriptB0y  
    OP
       2020-03-02 20:40:45 +08:00
    @lc1450 我没遇到过,你贴下报错我看看?是不是缺了 C 的编译工具链
    dragonszy
        13
    dragonszy  
       2020-03-10 20:13:36 +08:00
    在 PyOxidizer 的打包工具比较中发现了 Nuitka,应该是比较不错的工具。
    scriptB0y
        14
    scriptB0y  
    OP
       2020-03-11 00:08:49 +08:00
    @dragonszy 是的,Nutika 更加有希望一些,我理解是直接翻译+编译,比 PyOxidizer 更快
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1025 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:17 · PVG 06:17 · LAX 15:17 · JFK 18:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.