使用Pyinstaller打包时如何包含多个数据文件

8

我需要在pyinstaller的“onefile”可执行文件中包含一个DLL和一个文本文件。我可以添加DLL,但如果我尝试指定两个文件,pyinstaller会报错。我宁愿使用命令行选项(而不是spec文件)-多个文件的正确格式是什么?

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

尝试了一些方法,例如pyinstaller: error: argument --add-data: invalid add_data_or_binary value: '/C/path1/my.dll;/c/path2/my.txt;.'
3个回答

13

9

我不知道命令行需要哪种语法,但是您可以编辑生成的规范来包含数据路径,其中数据是元组列表。

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

所以,规范可能如下所示:
a = Analysis(['Frequency_Analysis_DataInput_Animation_cge.py'],
             pathex=['C:\\Users\\mousavin\\Documents\\Analysis'],
             binaries=[],
             datas=[('/path/file1', '.'), (/path/file2, '.')],
...

然后再使用指令重新构建

pyinstaller script.spec

1
为了在使用pyinstaller将多个文件添加到你的EXE文件中,最好的方法是将文件列表添加到你的应用程序规格(spec)文件中。
import glob

a = Analysis(['application.py'],
         pathex=['D:\\MyApplication'],
         binaries=[],
         datas=[],
         hiddenimports=[],
         hookspath=[],
         runtime_hooks=[],
         excludes=[],
         win_no_prefer_redirects=False,
         win_private_assemblies=False,
         cipher=block_cipher,
         noarchive=False)

a.datas += [("assets\\"+file.split("\\")[-1], file, "DATA") for file in glob.glob("D:\\MyApplication\\assets\\*")]

pyz = PYZ(a.pure, a.zipped_data,
         cipher=block_cipher)
exe = EXE(pyz,
      a.scripts,
      a.binaries,
      a.zipfiles,
      a.datas,
      [],
      name='MyApplication',
      debug=True,
      bootloader_ignore_signals=False,
      strip=False,
      upx=True,
      upx_exclude=[],
      runtime_tmpdir=None,
      console=True )

基本上,glob会读取assets文件夹中的所有文件,这里我只是使用列表推导式将所有要包含的文件附加到一起。
a.datas += [("assets\\"+file.split("\\")[-1], file, "DATA") for file in glob.glob("D:\\MyApplication\\assets\\*")]

这行代码将assets文件夹中的所有文件添加到你的应用程序的assets文件夹中。
这个解决方案对我有效。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接