I’ve created a flask executable using pyinstaller. The app has to read and write to a SQLite3 database. I am using flask-sqlalchemy and flask-migrate, flaskwebgui for opening in browser. The executable is unable to read the database.
Details:
Using the following spec file to create a standalone exe file
# -*- mode: python ; coding: utf-8 -*-
added_files = [
('main/templates/', 'templates'),
('main/static/', 'static')
]
a = Analysis(
['run.py'],
pathex=[],
binaries=[],
datas=added_files,
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
noarchive=False,
optimize=0,
)
pyz = PYZ(a.pure)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.datas,
[],
name='run',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=False,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
- The resulting
/dist
folder has an executable calledrun.exe
. I am able to run the exe which opens a browser window (using Flaskwebgui) and the index page loads normally but as soon as I try to open ‘all loans’ view it gives error
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: loan
. - The flask app is able to read/write to the database when run normally (command line) but it give error when running from the executable.
- In the root directory, the database file called
loansdb
is stored inside/instance
directory which was created by flask-migrate. - folder structure (overview only, not exact)
project/
|- main/
|- routes.py
|- __init__.py
|- models.py
|- templates/
|- index.html
|- static/
|- style.css
|- dist/
|- run.exe
|- logs/
|- instance/
|- loansdb
- The app reads the environment variables for the DATABASE_URL and I have verified that the executable is also able to read the environment variables.
- I put the
loansdb
file in the/dist
folder like:
project
|- dist
|- run.exe
|- loansdb
but it did not fix the problem.
- Similar to the above method, I put the whole
/instance
folder inside/dist
but that also had no effect. - I read up on this article talking about copying the temp files into the application’s directory but neither do I understand half of it and nor does it work.
Expected Behavior
- My understanding is that the database cannot be a part of the executable because that will make the database read-only because it runs in a ‘frozen’ state, which is not desired in this case.
- Being able to ship the program as a zip with the executable, database and other files such as a manual/readme would be most optimal for my use case.
- I am fairly new to flask and sqlalchemy and I would appreciate any alternative approach as well.
New contributor
JuveWatson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.