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

问题描述 投票:0回答:1

我创建了一个使用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_freeze之后有什么办法可以减少应用程序的大小吗?

谢谢!

python cx-freeze
1个回答
0
投票

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

我使用类似这样的脚本:

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命令后重新生成,您可以尝试删除这些文件,看看会发生什么。

© www.soinside.com 2019 - 2024. All rights reserved.