首页 > 解决方案 > linux上用pyinstaller打包的python程序可以在windows上运行吗?

问题描述

我的 python 打包文件在 Ubuntu Linux 上完美运行。我打开终端并输入

./[filename]

并且程序运行,但在 Windows 终端cmdpowershell. 我还尝试重命名文件以.exe使其可执行,但它也对我不起作用。

python另外,我没有pyinstaller安装在 Windows 机器上。

标签: pythonlinuxwindowspyinstallerpython-3.8

解决方案


不,linux 打包的 pyinstaller 程序不会在 windows 上运行,您必须获取脚本源并使用 pyinstaller 在 windows 上重新打包。因为 pysintaller 里面封装了可执行的二进制程序和共享库,在 Windows 和 Linux 中的格式不同。

pyinstaller 打包文件的内容是一种特殊的自定义 pysintaller 格式的 SFX 存档。我刚刚查看了 pyinstaller 的模块代码,并根据收到的知识实现了下一个简单的脚本来提取 pyinstaller 打包文件的所有内容,fname在脚本中提供:

import os, shutil
from PyInstaller.archive.readers import CArchiveReader

fname = 'z13.exe' # Provide packed filename here
ddir = fname + '_extracted/'

assert os.path.exists(fname), fname + ' not exists!'

if os.path.exists(ddir):
    shutil.rmtree(ddir)
os.makedirs(ddir, exist_ok = True)

r = CArchiveReader(fname)
for fname in r.contents():
    os.makedirs(ddir + os.path.dirname(fname), exist_ok = True)
    data = r.extract(fname)[1]
    with open(ddir + fname, 'wb') as f:
        f.write(data)

推荐阅读