Skip to main content
 首页 » 编程设计

python之当需要包含非 python 文件时,使用 PyInstaller 创建单个 exe

2025年02月15日12findumars

有没有一种方法可以使用 PyInstaller 创建一个包含文本文件和 .ui 文件等内容的可执行文件?我该怎么做呢?

请您参考如下方法:

您可以使用 spec file 将项目添加到 pyinstaller 构建的 exe 中:

For example, to add a single README file to the top level of a one-folder app, you could modify the spec file as follows:

a = Analysis(... 
     datas=[ ('src/README.txt', '.') ], 
     ... 
     ) 

You have made the datas= argument a one-item list. The item is a tuple in which the first string says the existing file is src/README.txt. That file will be looked up (relative to the location of the spec file) and copied into the top level of the bundled app.

The strings may use either / or \ as the path separator character. You can specify input files using “glob” abbreviations. For example to include all the .mp3 files from a certain folder:

a = Analysis(... 
     datas= [ ('/mygame/sfx/*.mp3', 'sfx' ) ], 
     ... 
     )