使用 pyinstaller 包含多个数据文件
Include multiple data files with pyinstaller
我需要在 pyinstaller "onefile" 可执行文件中包含一个 DLL 和一个文本文件。我可以只添加 DLL,但如果我尝试指定这两个文件,pyinstaller 会抱怨。我宁愿使用命令行选项(而不是规范文件)——多个文件的正确格式是什么?
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: 错误: 参数 --add-data: 无效 add_data_or_binary 值: '/C/path1/my.dll;/c/path2/my.txt;.'
答案在 https://media.readthedocs.org/pdf/pyinstaller/cross-compiling/pyinstaller.pdf 中,这表明我可以简单地多次使用 --add-data 选项!
我不知道命令行需要哪种语法,但是你
可以编辑生成的规范以包含数据的路径,其中数据是
元组列表。
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
为了使用 pyinstaller 将数据多个文件添加到 EXE 文件中,最好的方法是将文件列表添加到应用程序的规范文件中。
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 会读取资产文件中的所有文件,在这里我只是使用列表理解附加要包含的所有文件
a.datas += [("assets\"+file.split("\")[-1], file, "DATA") for file in glob.glob("D:\MyApplication\assets\*")]
此行将资产文件夹中的所有文件添加到应用程序的资产文件夹中。
这个解决方案对我有用。
我需要在 pyinstaller "onefile" 可执行文件中包含一个 DLL 和一个文本文件。我可以只添加 DLL,但如果我尝试指定这两个文件,pyinstaller 会抱怨。我宁愿使用命令行选项(而不是规范文件)——多个文件的正确格式是什么?
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: 错误: 参数 --add-data: 无效 add_data_or_binary 值: '/C/path1/my.dll;/c/path2/my.txt;.'
答案在 https://media.readthedocs.org/pdf/pyinstaller/cross-compiling/pyinstaller.pdf 中,这表明我可以简单地多次使用 --add-data 选项!
我不知道命令行需要哪种语法,但是你 可以编辑生成的规范以包含数据的路径,其中数据是 元组列表。
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
为了使用 pyinstaller 将数据多个文件添加到 EXE 文件中,最好的方法是将文件列表添加到应用程序的规范文件中。
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 会读取资产文件中的所有文件,在这里我只是使用列表理解附加要包含的所有文件
a.datas += [("assets\"+file.split("\")[-1], file, "DATA") for file in glob.glob("D:\MyApplication\assets\*")]
此行将资产文件夹中的所有文件添加到应用程序的资产文件夹中。
这个解决方案对我有用。