打包pyinstaller

安装:pip3 install pyinstaller -i https://pypi.douban.com/simple  

安装测试:  pyinstaller --version     显示pyinstaller版本

了解几个常用命令

参数 用处
-F 将程序打包成一个文件
-w 不带控制台---不能看出错信息
-i 添加程序图标

我们将需要打包的test.py文件放到桌面上,之后打开命令行窗口,cd到桌面目录,输入pyinstaller -F -w test.py

结束后我们会发现在桌面上多了两个文件夹和一个spec文件:而我们想要的可执行程序就在dist文件夹中,双击打开就是我们的程序了

上面打包的可执行文件使用的是默认的图标,我们接下来打包时添加-i参数来给程序加个图标(请注意该用法只对Windows系统有效):

将图标ico放在桌面,跟test.py同路径。同样打开命令行窗口,cd到桌面,输入pyinstaller -F -w -i tu.ico test.py 按回车开始打包

打包代码---推荐:

新建一个文件,执行以下代码:

#!/usr/bin/env python3
#  -*- coding: utf-8 -*-
from PyInstaller.__main__ import run

#  -F:打包成一个EXE文件
#  -w:不带CMD输出控制台.如果打包出错,需要查看错误信息,就需要这个cmd窗口
#  --paths:依赖包路径
#  --icon:图标
#  --noupx:不用upx压缩
#  --clean:清理掉临时文件

if __name__ == '__main__':
    opts = ['-F','-w',
            # '--paths=D:\Program Files\Python\Lib\site-packages\PyQt5\Qt\bin',
            # '--paths=D:\Program Files\Python\Lib\site-packages\jpype',
            # '--noupx',
            # '--clean',
            # '--hidden-import=numpy',
            '成绩分析.py']
    run(opts)

unable to find qt5core.dll on path--错误的解决:

新建 fix_qt_import_error.py 文件:

# Fix qt import error
# Include this file before import PyQt5
import os
import sys
import logging


def _append_run_path():
    if getattr(sys, 'frozen', False):
        pathlist = []

        # If the application is run as a bundle, the pyInstaller bootloader
        # extends the sys module by a flag frozen=True and sets the app
        # path into variable _MEIPASS'.
        pathlist.append(sys._MEIPASS)

        # the application exe path
        _main_app_path = os.path.dirname(sys.executable)
        pathlist.append(_main_app_path)

        # append to system path enviroment
        os.environ["PATH"] += os.pathsep + os.pathsep.join(pathlist)

    logging.error("current PATH: %s", os.environ['PATH'])


_append_run_path()

然后在主程序中导入 import fix_qt_import_error   #注意:要在pyqt模块之前导入

原文地址:https://www.cnblogs.com/liming19680104/p/10395028.html