Python 如何在使用cx_freeze冻结PyQt5时限制文件大小

Python 如何在使用cx_freeze冻结PyQt5时限制文件大小,python,cx-freeze,Python,Cx Freeze,我创建了一个小烧瓶应用程序,我使用PyQt5显示它,我想将它冻结到一个可执行文件中。对于PyQt方面的内容,我从互联网上复制了一个示例,并添加了我自己的小更改,包括以下导入: from PyQt5.QtCore import QUrl from PyQt5.QtWidgets import QApplication, QWidget from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage 当我在cx_freez

我创建了一个小烧瓶应用程序,我使用PyQt5显示它,我想将它冻结到一个可执行文件中。对于PyQt方面的内容,我从互联网上复制了一个示例,并添加了我自己的小更改,包括以下导入:

from PyQt5.QtCore import QUrl 
from PyQt5.QtWidgets import QApplication, QWidget 
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
当我在cx_freeze中冻结它时,我会得到一个大约300mb大小的巨大文件夹,这对我来说太大了。在我看来,cx_freeze包含了整个PyQt5模块。在cx_冻结后,有没有办法减少应用程序的大小


谢谢

从PyQt4迁移到PyQt5时,我遇到了相同的问题。看起来cx_Freeze想要嵌入整个Qt库,不仅仅是PyQt,但这通常是不必要的。 对于简单的程序,在PyQt5目录中去掉Qt目录就足够了(仅超过160mb)。 不过,有时仍然需要DLL:在我的程序中,我使用了QtMultimedia的音频属性,我发现PyQt5/Qt/plugins/audio中的库是允许音频播放所必需的。 一个好的方法是运行冻结的可执行文件,然后运行另一个脚本来检查流程所需的依赖关系

我使用的脚本与此类似:

import os, psutil

#set the base path of the freezed executable; (might change,
#check the last part for different architectures and python versions
basePath = 'c:\\somepath\\build\\exe.win32-3.5\\'
#look for current processes and break when my program is found;
#be sure that the name is unique
for procId in psutil.pids():
    proc = psutil.Process(procId)
    if proc.name().lower() == 'mytestprogram.exe':
        break

#search for its dependencies and build a list of those *inside*
#its path, ignoring system deps in C:\Windows, etc.
deps = [p.path.lower() for p in proc.memory_maps() if p.path.lower().startswith(basePath)]

#create a list of all files inside the build path
allFiles = []
for root, dirs, files in os.walk(basePath):
    for fileName in files:
        filePath = os.path.join(root, fileName).lower()
        allFiles.append(filePath)

#create a list of existing files not required, ignoring .pyc and .pyd files
unusedSet = set(allFiles) ^ set(deps)
unusedFiles = []
for filePath in sorted(unusedSet):
    if filePath.endswith('pyc') or filePath.endswith('pyd'):
        continue
    unusedFiles.append((filePath[len(basePath):], os.stat(filePath).st_size))

#print the list, sorted by size
for filePath, size in sorted(unusedFiles, key=lambda d: d[1]):
    print(filePath, size)

请注意,删除打印列表中列出的所有内容是不安全的,但它可以很好地提示您不需要的最大文件。我通常都保持原样,然后在创建安装程序时忽略不需要的文件,但由于在执行
build
命令后将再次重建输出目录,因此您可以尝试删除这些文件,看看会发生什么情况。

谢谢,它起到了作用!。但现在如何知道哪一个是安全的删除或不。你能给我一个提示吗?