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

Does pyinstaller have any parameters like gcc -static?

我有一个类似的问题:?

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

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

-F--onefile 参数应该可以满足您的要求。您可能需要查看 specs 文件并进行相应调整。

使用--onefile 会将其编译成(您猜对了)一个文件。您可以包含带有 --add-binary 参数的二进制文件。

文档中的这些页面可能包含有关所有参数的一些有用的详细信息:https://pyinstaller.readthedocs.io/en/stable/spec-files.html#adding-binary-files https://pyinstaller.readthedocs.io/en/stable/usage.html

来自Docker Minimal Image PyInstaller Binary File?'s commands,I get the links关于如何将python二进制转为静态的问题,类似于go应用程序演示,say hello world in scratch。

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

print("test")

然后,docker 使用 Dockerfile 构建:

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(不知道能不能看到):

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

PS:

通过我的测试

  • CMD 必须由 ['xxx'] 而非 xxx 直接写入。

  • /tmp 演示中需要目录。

  • 其他python应用程序未测试,只是有关打印的演示代码