简体   繁体   English

使用 pyinstaller 和 pysqlcipher 创建一个文件 exe 的问题

[英]Problem Creating One File exe with pyinstaller and pysqlcipher

I'm trying to create a one file.exe to run on any windows machine but I've hit a problem with pysqlcipher.我正在尝试创建一个 file.exe 以在任何 windows 机器上运行,但我遇到了 pysqlcipher 的问题。 I've gone back to some basic code that just creates a simple database with a key, on my dev machine all works fine whether I use the python file or the compiled exe.我已经回到了一些基本代码,这些代码只是用一个密钥创建了一个简单的数据库,在我的开发机器上,无论我使用 python 文件还是编译的 exe,都可以正常工作。 I seem to be missing a library, path or both?我似乎缺少库、路径或两者兼而有之? I've tried adding vaious items using --add-data but have spent hours and made no progress.我尝试使用 --add-data 添加各种项目,但花了几个小时但没有取得任何进展。 Here is the basic bit of python:-这是 python 的基本位:-

from pysqlcipher3 import dbapi2 as sqlite
import os
import sys

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

conn = sqlite.connect('test.db')
c = conn.cursor()
c.execute("PRAGMA key='password'")
c.execute('''create table stocks (date text, trans text, symbol text, qty real, price real)''')
c.execute("""insert into stocks values ('2006-01-05','BUY','RHAT',100,35.14)""")
conn.commit()
c.close()

When I run the exe on a different windows 10 PC I get this error当我在不同的 windows 10 PC 上运行 exe 时,出现此错误

Traceback (most recent call last): File "testdb.py", line 1, in File "c:\users\xxx\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module File "site-packages\pysqlcipher3-1.0.3-py3.8-win-amd64.egg\pysqlcipher3\dbapi2.py", line 33, in ModuleNotFoundError: No module named 'pysqlcipher3._sqlite3' [9248] Failed to execute script testdb回溯(最近一次调用):文件“testdb.py”,第 1 行,在文件“c:\users\xxx\appdata\local\programs\python\python38\lib\site-packages\PyInstaller\loader\pyimod03_importers. py”,第 623 行,在 exec_module 文件中“site-packages\pysqlcipher3-1.0.3-py3.8-win-amd64.egg\pysqlcipher3\dbapi2.py”,第 33 行,在 ModuleNotFoundError 中:没有名为 'pysqlcipher3._sqlite3 的模块' [9248] 无法执行脚本 testdb

The error references the path on my dev PC and also refers to line 33 in dbapi2.py which is:-该错误引用了我的开发 PC 上的路径,还引用了 dbapi2.py 中的第 33 行,即:-

from pysqlcipher3._sqlite3 import *

I have tried adding various files when running pyinstaller but I am making no progress, I'm sure its nothing simple but need help please.我尝试在运行 pyinstaller 时添加各种文件,但我没有取得任何进展,我确信这并不简单,但需要帮助。

I encountered a similar problem a while back and one thing that worked for me is adding the sqlite3 dll in the spec file of pyinstaller.不久前我遇到了类似的问题,对我有用的一件事是在 pyinstaller 的规范文件中添加 sqlite3 dll。 It is also possible to add it from command line.也可以从命令行添加它。 I had tried many other things before such as re-creating a conda environment and rebuilding my python package but that did not work.我之前尝试过很多其他的事情,比如重新创建一个 conda 环境并重建我的 python package 但这不起作用。 In your spec file where you add the pyinstaller binaries please try something like.在您添加 pyinstaller 二进制文件的规范文件中,请尝试类似的操作。

binaries=[('C:\\Users\\myname\\newfolder\\sqlite3.dll','.')]

You can download the DLL from https://www.sqlite.org/download.html depending on windows or linux platform. You can download the DLL from https://www.sqlite.org/download.html depending on windows or linux platform. Once you add the dll and recompile it should be fine.一旦你添加了 dll 并重新编译它应该没问题。 I think this error is primarily happening as dbapi2 requests the sqlite3 dll which in my case atlease was missing from the DLL folder.我认为此错误主要发生在 dbapi2 请求 sqlite3 dll 时,在我的情况下,DLL 文件夹中缺少该文件。 I just downloaded and added it in my spec file, but you can also try to add it to the hookspath or ENV folder.我刚刚下载并添加到我的规范文件中,但您也可以尝试将其添加到 hookspath 或 ENV 文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM