首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编译时不包括SubModules的Pyinstaller

编译时不包括SubModules的Pyinstaller
EN

Stack Overflow用户
提问于 2020-02-18 06:37:02
回答 1查看 632关注 0票数 0

我已经使用PyQt5开发了UI,现在我正在使用Pyinstaller将代码转换成独立的应用程序,但问题是文件大小越来越大(将近250 am )。我是否可以缩小文件的大小,但不包括不必要的PyQt5导入。在我用过的图书馆之后-

代码语言:javascript
复制
from PyQt5 import QtCore, QtGui, QtWidgets
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import os,sys
from mat4py import loadmat
from matplotlib import pyplot as plt

PyQt5库有许多子模块(QtCore、QtGui、QtWidgets、QtMultimedia、QtBluetooth、QtNetwork、QtPositioning、Enginio、QtWebSockets、QtWebKit、QtWebKitWidgets、QtXml、QtSvg、QtSql、QtTest),其中我只使用QtCore、QtGui和QtTest。

类似地,库matplotlib --我正在其中使用的许多模块--。

我想跳过程序不使用的模块,这样我就可以减少可执行文件的大小。

我的pyinstaller规范文件-

代码语言:javascript
复制
import sys
sys.setrecursionlimit(3000)

block_cipher = None

a = Analysis(['ReadMAT.py'],
             pathex=['C:\\Users\\Sekhar\\Documents\\PythonScripts\\RXP\\ReadMAT'],
             binaries=[],
             datas=[],
             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='ReadMAT',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=False, icon='ReadMAT.ico')

如何跳过不会进入编译过程的子模块。如何减少可执行文件的大小?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-18 09:22:06

PyInstaller对每个包都有一个钩子机制,它处理包所需的二进制文件。

对于一些著名的库,如Qt,它实现了一个有效的钩子文件,它只检索必要的二进制文件。但是,如果您想要排除某些部分,则需要通过使用exclude命令或操作钩子文件自行完成:

  1. 通过使用--exclude-module排除不必要的模块。通常,这就足够了。
  2. 通过修改特定的钩子文件并删除不需要的二进制文件。 例如,在Qt的钩子文件(<Pyinstaller_path>/utils/hooks/qt.py)中,有一个名为_qt_dynamic_dependencies_dict的变量,它具有位于<qt_installation_path>/Qt/bin中的所有二进制文件,因此您可以删除不需要的每个二进制文件。 稍后,在打包其他Qt二进制文件的名为get_qt_binaries的函数中,您可以像opengl32sw.dll一样删除不需要的每个二进制文件。

例如:

代码语言:javascript
复制
_qt_dynamic_dependencies_dict = {
    ## "lib_name":              (.hiddenimports,           translations_base,  zero or more plugins...)
    # I've removed qt5bluetooth with commenting below line
    #"qt5bluetooth":             (".QtBluetooth",           None,               ),  # noqa: E241,E202
    "qt5concurrent":            (None,                     "qtbase",           ),
    ...
}
...

def get_qt_binaries(qt_library_info):
    binaries = []
    angle_files = ['libEGL.dll', 'libGLESv2.dll', 'd3dcompiler_??.dll']
    binaries += find_all_or_none(angle_files, 3, qt_library_info)

    # comment the following two lines to exclude the `opengl32sw.dll`
    # opengl_software_renderer = ['opengl32sw.dll']
    # binaries += find_all_or_none(opengl_software_renderer, 1, qt_library_info)

    # Include ICU files, if they exist.
    # See the "Deployment approach" section in ``PyInstaller/utils/hooks/qt.py``.
    icu_files = ['icudt??.dll', 'icuin??.dll', 'icuuc??.dll']
    binaries += find_all_or_none(icu_files, 3, qt_library_info)

    return binaries
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60275109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档