将 Flask Web 应用程序转换为独立的可执行桌面应用程序

Ida*_*nah 6 python pyinstaller flask flask-sqlalchemy

我正在尝试使用 Pyinstaller 将 Flask Web 应用程序导出到独立的可执行文件以在本地主机上运行,​​但我在打包数据库文件时遇到问题。

我的项目结构:

/project
    /app
        __init__.py    #    This is where I call the app.run() method
        routes.py
        forms.py
        config.py

        /database
            models.py
            data.db    #    sqlalchemy db file

        /files    #    Folder that stores temporary files
        /templates    #    HTML files

    /backend
        __init__.py
        module1.py
        module2.py

Run Code Online (Sandbox Code Playgroud)

在 /project/app/ 中,我使用以下命令运行 Pyinstaller:

pyinstaller -F --add-data "templates;templates" --add-data "database;database" __init__.py
Run Code Online (Sandbox Code Playgroud)

执行上述命令后,尝试运行生成的 exe 文件失败,并在控制台上显示以下错误:

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file
Run Code Online (Sandbox Code Playgroud)

在 config.py 中,我有一个名为 Config 的类,在其中定义 db URI,如下所示:

SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join('database', 'data.db')
Run Code Online (Sandbox Code Playgroud)

我相信问题出在实际的打包上,因为在 pycharm 中或从命令行运行应用程序没有问题。

非常感谢帮助解决这个问题,提前非常感谢!