Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

kivy - 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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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", "."],
    ...
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...