I’ve been writting some code to create an easy app to register products using python 3.7.3
and sqlite3
on debian. When I’m finally done, I tried to create an executable file from my script using pyinstaller. Everything was fine until I run the executable file by command using "./file" in the dist folder and there were 3 errors. On my sqlite database I used just a table called "product" but the terminal shows me:
sqlite3.OperationalError: no such table: product
Additionally it shows me other messages:
Traceback (most recent call last):
File "index.py", line 141, in <module>
File "index.py", line 47, in __init__
File "index.py", line 61, in get_products
File "index.py", line 52, in run_query"
On terminal I used this command to run pyinstaller :
pyinstaller --add-data "database.db:." index.py --onefile
Why my database is not recognized to run as onefile?
You can see my code on https://github.com/BraymNoodles/AppPythonProducts.git
2
Answers
When you run the executable, it will decompress itself to a path like
_MEIxxxxx
(where xxxxx is a random number) inside temporary path (environment variable TEMP). The filedatabase.db
is within that directory.So if your code opens the database file like below:
It will look for
database.db
in current directory, but it does not exist. So a newdatabase.db
will be created and this causes the error mentioned in your question:because
database.db
is empty.You need to get the temporary path by using below code:
and open the database like below:
You can read more information about the temporary path in PyInstaller document.
Use this if you are trying to connect sqlite3 to a one file executable with no console.