Pyinstaller打包数据文件或配置文件的简单说明 |
您所在的位置:网站首页 › python数据文件输入输出 › Pyinstaller打包数据文件或配置文件的简单说明 |
官网:http://www.pyinstaller.org/ PyInstaller freezes (packages) Python applications into stand-alone executables, under Windows, GNU/Linux, Mac OS X, FreeBSD, Solaris and AIX. 我本地程序文件说明:同一个目录【E:\pytest\student】下两个文件,一个.py,一个.txt文本文件;py程序读写txt文件。 我们的py程序也许用到了其他配置文件(properties/conf),如何将这些文件也打包到执行程序中呢? 安装pip install pyinstaller Quickstart
在spec文件中,手工写入数据文件等路径,然后通过spec生成可执行程序。 0、生成spec文件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. 可以使用下面两种方式,推荐第二种 pyinstaller yourprogram.py 或 pyinstaller -makespec yourprogram.py 1、文件夹形式的可执行程序-D, Create a one-folder bundle containing an executable (default) pyinstaller -D student.py
使用spec文件执行生成可执行程序命令 pyinstaller student.spec基本步骤同上,仅需将参数-D,调整为-F(pyinstaller -F student.py) 但因为单个exe文件类似zip文件,运行时有解压动作。故此需要修改源码,即:读写txt文件代码。 文件夹形式的源代码: open('students.txt', 'a')单文件形式的源代码: open(resource_path('students.txt'), 'a') def resource_path(relative_path): """ Get absolute path to resource, works for dev and for PyInstaller """ base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__))) return os.path.join(base_path, relative_path)本文示例使用的是文本文件,如果二进制文件怎么处理?更多spec文件用法,大家参考官方: https://pyinstaller.readthedocs.io/en/stable/spec-files.html |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |