gpt4 book ai didi

python - sqlite + PyQt5 到独立 exe - Python 3.6.3 + Pyinstaller

转载 作者:太空宇宙 更新时间:2023-11-03 14:33:35 24 4
gpt4 key购买 nike

我想使用 Pyinstaller 创建一个 exe,其中包含数据库 (.db) 和图片 (.png)。我希望所有内容都变成单个 exe (--onefile)。我尝试直接在 spec 文件中添加这两个元素的路径,但它不起作用。

这是我的spec 文件:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['back_end.py'],
pathex=['C:\\Users\\...\\site-packages\\PyQt5\\Qt\\bin', 'C:\\Users\\...\\Test_packaging'],
binaries=[],
datas=['C:\\Users\...\\Test_packaging\\database1.db', 'C:\\Users\...\\Test_packaging\\picture1.png'],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='back_end',
debug=False,
strip=False,
upx=True,
runtime_tmpdir=None,
console=False )

谢谢

最佳答案

您必须解决许多问题才能使其正常工作。例如:

  • 获取正确的资源路径
  • 添加数据

第一个问题通过根据执行模式调整路径来解决。

def app_path(path):
frozen = 'not'
if getattr(sys, 'frozen', False):
# we are running in executable mode
frozen = 'ever so'
app_dir = sys._MEIPASS
else:
# we are running in a normal Python environment
app_dir = os.path.dirname(os.path.abspath(__file__))
return os.path.join(app_dir, path)

第二个问题是有效地包含您需要的内容。起初,明显的解决方案是手动添加每个图像和数据库,但我有很多图像。我转向规范文件方式,使用通配符运算符 (*) 在文件夹中添加我需要的内容,而不是添加 folder/*

added_files = [
( './pics/*', 'pics' ),
( './db/*', 'db' ),
]

然后在分析中,

datas = added_files

完整的答案很长。我写过this article以一些细节来展示我为解决问题所经历的事情。

关于python - sqlite + PyQt5 到独立 exe - Python 3.6.3 + Pyinstaller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47114772/

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