找回密码
 立即注册
搜索
查看: 284|回复: 0

使用 pyinstaller 打包程序为单 exe 应用

[复制链接]

266

主题

0

回帖

1119

积分

管理员

积分
1119
发表于 2024-3-2 14:59:12 | 显示全部楼层 |阅读模式

一般的程序打包:

pyinstaller -F main.py

如果包含 html、js 等文件需要特别处理一下。

待打包示例源代码 main.py:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 生成资源文件目录访问路径
def resource_path(relative_path):
    if getattr(sys, 'frozen', False):  # 是否Bundle Resource
        base_path = sys._MEIPASS
    else:
        base_path = os.path.abspath(".")
    return os.path.join(base_path, relative_path)

filename = resource_path(os.path.join("web", "mesh.html"))
# 访问 html 文件用 filename 就可以了
print(filename)

打包文件:

首先生成 spec 文件:

pyi-makespec -F main.py

会生成 main.spec 文件,编辑它:

datas 添加 ('web', 'web'),web 文件夹存放了 html、js 等文件,这个文件夹会被打包进去。

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None

a = Analysis(['main.py'],
             pathex=['E:\\projects\\python\\test'],
             binaries=[],
             datas=[('web', 'web')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='main',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=True )

开始打包:

pyinstaller -F main.spec

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|棱讯科技 ( 粤ICP备2024228160号-2|粤公网安备44030002003510号 )

GMT+8, 2024-7-27 19:03 , Processed in 0.015152 second(s), 3 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表