Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include multiple data files with pyinstaller

Tags:

pyinstaller

I need to include a DLL AND a text file in a pyinstaller "onefile" executable. I can add just the DLL but if I try to specify both files, pyinstaller complains. I would rather use the command line options (rather than the spec file) - what's the correct format for multiple files?

http://pyinstaller.readthedocs.io/en/stable/spec-files.html#adding-data-files

http://pyinstaller.readthedocs.io/en/stable/usage.html#options-group-what-to-bundle-where-to-search

Tried a few things, e.g. pyinstaller: error: argument --add-data: invalid add_data_or_binary value: '/C/path1/my.dll;/c/path2/my.txt;.'

like image 606
jqwha Avatar asked Dec 14 '17 10:12

jqwha


People also ask

How do I add multiple data to PyInstaller?

In order to add data multiple files into your EXE file using pyinstaller, the best way is to add the list of files into your application's spec file. This line adds all the files inside assets folder inside your application's assets folder.

Does PyInstaller include all dependencies?

PyInstaller can bundle your script and all its dependencies into a single executable named myscript ( myscript.exe in Windows). The advantage is that your users get something they understand, a single executable to launch.

Which is better PyInstaller or py2exe?

In PyInstaller it is easy to create one exe, By default both create a bunch of exes & dlls. In py2exe its easier to embed manifest file in exe, useful for run as administrator mode in windows vista and beyond. Pyinstaller is modular and has a feature of hooks to include files in the build that you like.

Does PyInstaller bundle Python?

PyInstaller bundles a Python application and all its dependencies into a single package. The user can run the packaged app without installing a Python interpreter or any modules. PyInstaller reads a Python script written by you.

What is Pathex in PyInstaller?

pathex is an optional list of paths to be searched before sys.path. Source: https://github.com/pyinstaller/pyinstaller/blob/develop/PyInstaller/building/build_main.py#L123. The spec file is actually executable Python code. PyInstaller builds the app by executing the contents of the spec file.


2 Answers

The answer was in https://media.readthedocs.org/pdf/pyinstaller/cross-compiling/pyinstaller.pdf which indicates I can simply use the --add-data option multiple times!

like image 163
jqwha Avatar answered Jan 04 '23 05:01

jqwha


I do not know which syntax is necessary for the command line, but you can edit the generated spec to include the path to to data, where data is a list of tuples.

datas = [('/path/to/file', '/path/in/bundle').
          (...) ]

So the spec might look as follows:

a = Analysis(['Frequency_Analysis_DataInput_Animation_cge.py'],
             pathex=['C:\\Users\\mousavin\\Documents\\Analysis'],
             binaries=[],
             datas=[('/path/file1', '.'), (/path/file2, '.')],
...

and then build again with

pyinstaller script.spec
like image 28
Nima Mousavi Avatar answered Jan 04 '23 05:01

Nima Mousavi