Add configuration file outside PyInstaller –onefile EXE file into dist directory

A repository on GitHub helped me to find a solution to my question.

I’ve used the shutil module and .spec file to add extra data files (in my case, a config-sample.ini file) to dist folder using the PyInstaller --onefile option.

Make a .spec file for PyInstaller

First of all, I’ve created a makespec file with the options I needed:

pyi-makespec --onefile --windowed --name exefilename scriptname.py

This command creates an exefilename.spec file to use with PyInstaller.

Modify exefilename.spec, adding shutil.copyfile

Now I’ve edited the exefilename.spec, adding the following code at the end of the file.

import shutil

shutil.copyfile('config-sample.ini', '{0}/config-sample.ini'.format(DISTPATH))
shutil.copyfile('whateveryouwant.ext', '{0}/whateveryouwant.ext'.format(DISTPATH))

This code copies the data files needed at the end of the compile process.
You could use all the methods available in the shutil package.

Run PyInstaller

The final step is to run the compile process

pyinstaller --clean exefilename.spec

The result is that in the dist folder you should have the compiled .exe file together with the data files copied.

Consideration

In the official documentation of PyInstaller I didn’t find an option to get this result. I think it could be considered as a workaround… that works.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)