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

sqlite + PyQt5 to standalone exe - Python 3.6.3 + Pyinstaller(sqlite + PyQt5 到独立 exe - Python 3.6.3 + Pyinstaller)
本文介绍了sqlite + PyQt5 到独立 exe - Python 3.6.3 + Pyinstaller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 Pyinstaller 创建一个 exe,包括一个数据库 (.db) 和一个图片 (.png).我希望将所有内容都放入一个 single exe (--onefile).我试图直接在 spec 文件中添加两个元素的路径,但它不起作用.

I would like to create an exe with Pyinstaller including a database (.db) and a picture (.png). I want everything into a single exe (--onefile). I tried to add the path of both elements directly in the spec file but it doesn't work.

这是我的 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 )

谢谢

推荐答案

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

You have to solve many issues to get this working. For example:

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

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

The first issue is solved by adjusting paths depending on execution mode.

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/*.

The second issue is efficiently including what you need. At first the obvious solution is to add each image and db manually, but i had lots of image. I turned to the spec file way using the wildcard operator (*) to add what i need in folder than adding folder/*.

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

然后在分析中,

datas = added_files

一个彻底的答案很长.我已经写了 这篇文章 显示我为解决问题所经历的一些细节.

A thorough answer is quite long. I've written this article to show in some minute details what i went through to solve the issue.

这篇关于sqlite + PyQt5 到独立 exe - Python 3.6.3 + Pyinstaller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐