gpt4 book ai didi

python - pyinstaller 有没有像 gcc -static 这样的参数?

转载 作者:行者123 更新时间:2023-12-02 18:17:45 28 4
gpt4 key购买 nike

我有一个类似的问题:Is there a way to compile a Python program to binary and use it with a Scratch Dockerfile

在此page ,我看到有人说用-static编译C应用程序运行良好。

所以我有一个新问题:pyinstaller 是否有像 gcc -static 这样的参数来使 python 应用程序在 Scratch Docker 镜像中运行良好?

最佳答案

来自问题Docker Minimal Image PyInstaller Binary File?的命令,我得到 links关于如何将 python 二进制文件变为静态文件,就像 go 应用程序演示一样,从头开始说 Hello World 。

我做了一个简单的演示,app.py:

print("test")

然后,使用 Dockerfile 进行 docker 构建:

FROM bigpangl/python:3.6-slim AS complier
WORKDIR /app
COPY app.py ./app.py

RUN apt-get update \
&& apt-get install -y build-essential patchelf \
&& pip install staticx pyinstaller \
&& pyinstaller -F app.py \
&& staticx /app/dist/app /tu2k1ed

FROM scratch
WORKDIR /
COPY --from=complier /tu2k1ed /
COPY --from=complier /tmp /tmp
CMD ["/tu2k1ed"]

得到下图,才7.22M(不知道能不能看到):

enter image description here

尝试通过代码docker run test运行,成功:

enter image description here

附言:

通过我的测试

  • CMD 必须写成 ['xxx'] 而不是 xxx 直接写。

  • /tmp 目录在演示中是必需的。

  • 其他python应用未测试,仅提供关于打印的演示代码

关于python - pyinstaller 有没有像 gcc -static 这样的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62951276/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com