问题描述
在开发过程中,我们经常需要引入静态资源,在使用Pyinstaller打包时,这些静态资源需要使用--add-data
参数打包进可执行文件,但在Pyinstaller 6.0.0以后,--add-data
参数中的文件会被默认打包进_internal
文件夹
官方文档
解决方法
方法一:使用__file__定位静态资源
这里以一段pywebview
代码为例
这是修改前的代码
import webview
if __name__ == '__main__':
webview.create_window('Hello world', 'assets/index.html')
webview.start()
修改后:
import webview
import os
if __name__ == '__main__':
webview.create_window('Hello world', os.path.join(
#用__file__与相对路径拼接
os.path.dirname(__file__), 'assets/index.html'))
webview.start()
然后用pyinstaller打包
pyinstaller -w -F --add-data assets;assets -n test main.py
方法二:使用–contents-directory参数
--contents-directory
可以指定静态资源文件目录,我们指定--contents-directory .
就可以像旧版本一样打包了
pyinstaller -w -F --add-data assets;assets -n test --contents-directory . main.py