【问题标题】:can python program packaged with pyinstaller on linux runs on windows?linux上用pyinstaller打包的python程序可以在windows上运行吗?
【发布时间】:2021-01-17 19:59:48
【问题描述】:
我的 python 打包文件可以在 Ubuntu Linux 上完美运行。我打开终端并输入
./[filename]
程序运行,但在 Windows 终端 cmd 和 powershell 上没有发生同样的事情。我还尝试将文件重命名为 .exe 以使其可执行,但它也对我不起作用。
另外,我没有在 Windows 机器上安装 python 和 pyinstaller。
【问题讨论】:
标签:
python
linux
windows
pyinstaller
python-3.8
【解决方案1】:
不,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)