分发嵌入 Cython 编译的代码并使其在任何机器上运行所需的最少文件集

Minimal set of files required to distribute an embed-Cython-compiled code and make it work on any machine

TL;DR:如何使用 Cython 作为分发方法而不是 Py2exe、cx_freeze、pyinstaller 等


Making an executable in Cython 之后,我想看看如何将 Python 程序分发给任何 Windows 用户(没有 Python 已经安装在他的机器上)首先用 Cython --embed.

编译它

让我们使用 test.py:

import json
print(json.dumps({'key': 'hello world'}))

并编译它:

cython test.py --embed
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x64
cl test.c /I C:\Python37\include /link C:\Python37\libs\python37.lib

它可以运行并生成 140KB test.exe 可执行文件。

运行 test.exe 在另一台机器上不能开箱即用,它需要:

即使这样,它仍然不起作用(下面的屏幕截图而不是 copy/paste 因为我没有在 VM 中管理 copy/paste - 这里离题):

ModuleNotFoundError: No module named 'encodings'

问题:分发 --embed-Cython 编译的代码并使其在任何机器上运行所需的最小文件集是什么(之前没有安装 Python它)?

经过进一步研究(我在一个空的 Win 7 x64 位虚拟机中尝试过,之前没有安装任何 VCredist),似乎这些文件就足够了:

  • 程序本身,test.exe(由cython --embed制作并用cl.exe编译)

  • python37.dll

  • python37.zip 来自 Windows x86-64 可嵌入 zip 文件 in https://www.python.org/downloads/windows/

  • vcruntime140.dll,前面 or ask the user to install vc_redist.x64.exe提到

  • ucrtbase.dll

  • 还需要 30 多个文件 api-ms-win-*.dll;如果不是,您将遇到以下错误:

    ... api-ms-win-crt-runtime-l1-1-0.dll is missing ...

备注:

  • 如果您需要另一个库,例如 pygame,copy/paste 来自 C:\Python37\Lib\site-packages\pygame 的文件夹似乎可以工作

  • 对我来说,concrt140.dll、msvcp140.dll、vccorlib140.dll似乎没有必要