Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyinstaller adding data files

I'm struggling with pyinstaller. Whenever I build this specific script with a kivy GUI and a .kv file, and run the .exe after the build, I get a fatal error:

IOError: [Errno 2] No such file or directory: 'main.kv' 

I've tried adding the .kv file, as well as a mdb and dsn file (for pypyodbc) using --add-data, but I get an error: unrecognized arguments: --add-data'main.kv'. (There were more --add-data arguments for the other files mentioned.)

Are there any solutions for this or maybe alternative methods?

like image 239
staos2 Avatar asked Jan 26 '17 09:01

staos2


People also ask

How do I add data to PyInstaller?

Getting back to adding data files We need to add data files such as help file or icon file or README files to pyinstaller. That's pretty simple. Just use the add-data flag while building your binaries. The separator is different for windows and posix based systems.

How do you add binary to PyInstaller?

You can add binary files to the bundle by using the --add-binary command option, or by adding them as a list to the spec file. In the spec file, make a list of tuples that describe the files needed. Assign the list of tuples to the binaries= argument of Analysis.

What is Pathex in PyInstaller?

Explanation. 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

As others (@Anson Chan, @schlimmchen) have said:

If you want to add some extra files, you should use Adding Data Files.

Two ways to implement

  • Command Line: add parameter to --add-data
  • Spec file: add parameter to datas=
    • Generated when running pyinstaller the first time.
      • Then later you can edit your *.spec file.
      • Then running pyinstaller will directly use your *.spec file.

Parameter Logic

Parameter in --add-data or datas=:

  • --add-data:
    • format: {source}{os_separator}{destination}
      • os_separator:
        • Windows: ;
        • Mac/Linux/Unix: :
      • source and destination
        • Logic:
          • source: path to single or multiple files, supporting glob syntax. Tells PyInstaller where to find the file(s).
          • destination file or files: destination folder which will contain your source files at run time. * NOTE: NOT the destination file name.
            • folder: destination folder path, which is RELATIVE to the destination root, NOT an absolute path.
    • Examples:
      • Single file: 'src/README.txt:.'
      • multiple files: '/mygame/sfx/*.mp3:sfx'
      • folder: /mygame/data:data'
  • datas=
    • Format: list or tuple.
    • Examples: see the following.
added_files = [     ( 'src/README.txt', '.' ),     ( '/mygame/data', 'data' ),     ( '/mygame/sfx/*.mp3', 'sfx' ) ]  a = Analysis(...     datas = added_files,     ... ) 

Your case

For your (Windows OS) here is:

  • --add-data in command line
    • pyinstaller -F --add-data "main.kv;." yourtarget.py

OR:

  • datas= in yourtarget.spec file, see following:
a = Analysis(...     datas = ["main.kv", "."],     ... ) 
like image 115
crifan Avatar answered Sep 18 '22 00:09

crifan


If you check pyinstaller -h for help, you can find --add-data option works like this [--add-data <SRC;DEST or SRC:DEST>]. So in your case try

pyinstaller -F --add-data "main.kv;main.kv" yourtarget.py 
like image 40
Anson Chan Avatar answered Sep 21 '22 00:09

Anson Chan