Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make pyinstaller include my database?

I've got a PyQt4 application which makes use of sqlite3. I have two seperate files currently: a .py file and a .db file. Originally I took the .py file and tried to create an executable just using that thinking maybe it would link somehow but when I run the .exe it breaks when it gets to database related code. How do I get pyinstaller to include the database as well?

like image 605
WewLad Avatar asked May 27 '16 12:05

WewLad


2 Answers

You need to use a specfile. Have a look at the documentation.

pyinstaller creates those file automatically. You just need to edit it. Have a look at the example from the docs. To add a README file you just need to add a tuple (source, destination) to 'datas':

a = Analysis(...
 datas=[ ('src/README.txt', '.') ],
 ...
 )
like image 74
Daniel Avatar answered Sep 25 '22 17:09

Daniel


This post helped me. Basically, you can run the pyinstaller command with arguments as follows (assuming your script is script.py and your database is database.db):

pyinstaller -w --add-data "database.db;." script.py

This will first create a .spec file that lists database.db as a file to be included in your build. The created .spec file will then be used to run the build.

As explained here, the -w option prevents a console from being displayed when you run the .exe file that will be created.

---- Alternatively ----

According to the docs, you can first create just the .spec file and then modify it to list the data files that should be included. The .spec file for script.py is created using:

pyi-makespec script.py

Then, within the .spec file created, you can modify the datas field:

a = Analysis(...
    datas=[ ('database.db', '.') ],
    ...
)

This assumes that database.db is in the root folder. If not, you will need to include the path. The '.' specifies where you want the database file to be located in the build (in this case, '.' is the root folder). To include more files, you can add more tuples ('<file>', '<path>') to the datas list.

like image 29
Joel Oduro-Afriyie Avatar answered Sep 21 '22 17:09

Joel Oduro-Afriyie