skip to Main Content

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


  1. 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 file database.db is within that directory.

    So if your code opens the database file like below:

    conn = sqlite3.connect('database.db')
    

    It will look for database.db in current directory, but it does not exist. So a new database.db will be created and this causes the error mentioned in your question:

    sqlite3.OperationalError: no such table: product

    because database.db is empty.

    You need to get the temporary path by using below code:

    import os
    import sys
    ...
    appdir = getattr(sys, '_MEIPASS', os.path.abspath(os.path.dirname(__file__)))
    

    and open the database like below:

    conn = sqlite3.connect(os.path.join(appdir, 'database.db'))
    

    You can read more information about the temporary path in PyInstaller document.

    Login or Signup to reply.
  2. base_path = os.path.dirname("**sys.executable**")
    conn sqlite3.connect(os.path.join(base_path, 'database.db')
    

    Use this if you are trying to connect sqlite3 to a one file executable with no console.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search