这个问题在这里已有答案:
我在同一个目录中使用其他.txt文件构建了一个刽子手游戏,其中包含hangman游戏中的单词bank,这些是从hangman.py文件中打开的,当我使用pyinstaller打包我使用path\to\pyinstaller.exe hangman.py --onefile
但这个不允许在可执行文件中使用.txt文件。如何打包.txt文件以便可执行文件正常运行?
您可以使用
pyinstaller --add-data 'path/to/file.txt:path/inside/exe' hangman.py
或者用文件列表编辑spec文件,即
added_files = [
( 'src/README.txt', '.' )
( '/mygame/sfx/*.mp3', 'sfx' ),
]
a = Analysis(...
datas = added_files,
...
)