相对路径不适用于credentials.Certificate()

问题描述

我在我的 Python 项目中使用 Firebase。要初始化 credential.Certificate() 我想使用这样的相对路径:

cred_path = 'jsonfile.json'
cred_path = os.path.join(os.path.dirname(__file__),cred_path)

输出

C:\Users\testuser\Desktop\Rewrite\jsonfile.json

如果我将路径传递给 firebase 方法并使用 pyinstaller 将其转换为 .exe,则会出现错误

 cred = credentials.Certificate(m)
firebase_admin.initialize_app(cred,{
    'databaseURL' : 'databaseurl.com'
})

回溯(最近一次调用最后一次):文件“rewriteword.py”,第 24 行, 在文件“firebase_admin\credentials.py”第 82 行中 init FileNotFoundError: [Errno 2] 没有这样的文件或目录:'jsonfile.json' [18292] 无法执行脚本 testscript

如果我使用绝对路径,它可以完美运行,没有错误。为什么会发生这种情况?

解决方法

修复了它。我不得不将 .json 数据添加到 pyinstaller。

cred_path = 'jsonfile.json'
bundle_dir = getattr(sys,'_MEIPASS',path.abspath(path.dirname(__file__)))
path_to_dat = path.abspath(path.join(bundle_dir,cred_path))
pyinstaller --onefile --add-data "jsonfile.json;." rewriteword.py

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...