pyinstaller 打包图片资源文件

来源:互联网 发布:react css in js 编辑:程序博客网 时间:2024/05/16 04:57

背景

最近空闲之余,在使用wxpython写桌面应用程序,虽然界面确实不敢恭维,但是使用python的好处还是很多的,所以网上研究了下想要将py文件转换成exe的话需要用到pyinstaller ,当然还有其他的工具也是可以做到这个的,类似于py2exe等等,这里我就不介绍其他工具是怎么使用的,因为百度都是一大把的东西了。但是后来发现,使用pyinstaller path/xx.py 命令打包出现的文件里面不包含我需要的一些ico.png等等文件,那这个就比较麻烦了,因为每次打包成一个exe的时候,我必须还得手动去工程目录下,将对应的资源文件拷贝到我打包的路径下。所以特定google了一把,嗯 确实找到解决方法。

正文

其实想要找到解决方法,最好就是去官网看使用文档,没有什么东西会比它来的更方便了,所以我们先从官方的使用手册入手吧。找到我们需要的地方Using Spec Files,这里我们可能先得了解下Spec file是什么东西。这里我们看看官网的解释

the first thing PyInstaller does is to build a spec (specification) file myscript.spec. That file is stored in the –specpath= directory, by default the current directory.

The spec file tells PyInstaller how to process your script. It encodes the script names and most of the options you give to the pyinstaller command. The spec file is actually executable Python code. PyInstaller builds the app by executing the contents of the spec file.

For many uses of PyInstaller you do not need to examine or modify the spec file. It is usually enough to give all the needed information (such as hidden imports) as options to the pyinstaller command and let it run

以上的内容实际是告诉我们 我们在使用pyinstaller path/xx.py的时候,实际上如果你不指定–specpath的话默认会在当前的路径下生成一个spec文件,这个文件会告诉pyinstaller如何处理你的脚本,pyinstaller创建一个exe的文件就是依靠它里面的内容进行执行的。正常情况下你不需要去修改这个spec文件,除非你需要打包一个dll或者so文件或者其他数据文件时。

这个时候我们就需要先通过

pyi-makespec options name.py [other scripts ...]

来生成一个spec文件,再来就是增加我们需要的资源文件。
这里写图片描述
红色的框就是新增的资源文件
下来增加完以后 我们只要pyinstaller options name.spec 就可以将我们需要的资源文件打包到生成的文件目录下了。

1 0
原创粉丝点击