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

start
PyInstaller analyzes yourprogram.py and:

  • Writes yourprogram.spec in the same folder as the script(生成spec文件)
  • Creates a folder build in the same folder as the script if it does not exist
  • Writes some log files and working files in the build folder
  • Creates a folder dist in the same folder as the script if it does not exist
  • Writes the yourprogram executable folder in the dist folder
    In the dist folder you find the bundled app you distribute to your users

数据(或配置)文件

在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
手工修改上述spec文件,仅调整datas列表参数,其他地方不动。
spec datas
修改说明:
Each tuple has two values, both of which must be strings:

  • The first string specifies the file or files as they are in this system now
  • The second specifies the name of the folder to contain the files at run-time

使用spec文件执行生成可执行程序命令

pyinstaller student.spec

install

2、单个文件形式的可执行程序

基本步骤同上,仅需将参数-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

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用PyInstaller打包Django应用程序时,你需要注意一些特殊的配置。下面是一个基本的步骤: 1. 确保你已经安装了PyInstaller: ``` pip install pyinstaller ``` 2. 进入你的Django项目的根目录,并创建一个新的目录用于存放打包后的文件: ``` mkdir dist ``` 3. 在项目根目录下创建一个名为`myapp.spec`的文件,用于配置PyInstaller打包选项。在该文件中添加以下内容: ``` # myapp.spec import django import os from PyInstaller.utils.hooks import collect_submodules hiddenimports = collect_submodules('django') a = Analysis(['manage.py'], pathex=[os.getcwd()], hiddenimports=hiddenimports, hookspath=None) a.datas += [ (os.path.join(django.__path__[0], 'conf', 'project_template', 'manage.py'), 'django/conf/project_template/manage.py') ] pyz = PYZ(a.pure, a.zipped_data, cipher=None) exe = EXE(pyz, a.scripts, a.binaries, a.zipfiles, a.datas, [], name='manage', debug=False, bootloader_ignore_signals=False, strip=False, upx=True, runtime_tmpdir=None, console=True ) ``` 4. 在项目根目录下执行以下命令,使用`myapp.spec`文件进行打包: ``` pyinstaller myapp.spec ``` 5. 打包完成后,在`dist`目录下会生成可执行文件。你可以将整个`dist`目录复制到其他机器上运行你的Django应用程序。 请注意,上述步骤只是基本配置,实际情况可能因项目结构和依赖项而有所不同。你可能还需要在`myapp.spec`文件中添加其他的隐藏导入项和数据文件,以确保打包后的应用程序能够正常运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值