使用 cx_Freeze、PyQt5、Python3 构建的 exe 无法导入 ExtensionLoader_PyQt5_QtWidgets.py 和 运行

exe built with cx_Freeze, PyQt5, Python3 can't import ExtensionLoader_PyQt5_QtWidgets.py and run

我正在尝试使用 cx_Freeze 为 Windows 打包我的应用 Python3、PyQt5。 我已经安装了 Python 3.4.3、Python-win32、PyQT5、cxfreeze。 应用程序本身,运行 在控制台中工作正常。

我试着用cx_freeze打包: python setup.py build_exe。 它在同一台主机上工作。但是当我将它移动到另一个全新安装的 WinXP 时,它给出了一个错误:

Traceback:
File "C:\Python34\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27 in <module>
File "pyftp1.py" in 7, in <module>
File "c:\python-bit.4\lib\importlib\_bootstrap.py", in 2237, in _find_and_load
File "c:\python-bit.4\lib\importlib\_bootstrap.py", in 2226, in _find_and_load_unlocked
File "c:\python-bit.4\lib\importlib\_bootstrap.py", in 1191, in _load_unlocked
File "c:\python-bit.4\lib\importlib\_bootstrap.py", in 1161, in _load_backward_compatible
File "ExtensionLoader_PyQt5_QtWidgets.py", line 22, in <module>
File "ExtensionLoader_PyQt5_QtWidgets.py", line 14, in __bootstrap__ 
ImportError: DLL load failed: Не найден указанный модуль.

英语:"specified module was not found".

这是我的 setup.py:

import sys
from cx_Freeze import setup, Executable

includes = ['sys', 'PyQt5', 'PyQt5.Core', 'PyQt5.QtGui', 'PyQt5.QtWidgets', 'os', 'os.path', 'ftplib',
    'traceback', 'time',]
excludes = []
packages = ['os', 'PyQt5']
path = []
build_exe_options = {
   'includes': includes,
   'excludes': excludes,
   'packages': packages,
   'path'    : path,
   #'dll_includes': ['msvcr100.dll'],
   'include_msvcr' : True,
   'include_files': [
       (r'C:\Windows\System32\msvcr100.dll', 'msvcr100.dll'),
   ],

}


base = None
if sys.platform == 'win32':
    base = 'Win32GUI'

setup(
    name = "pyftp1",
    version = "0.1",
    description = "Foto uploader",
    options = {'build_exe_options': build_exe_options},
    executables = [Executable("pyftp1.py", base=base, appendScriptToLibrary=False, copyDependentFiles=True)]
)

我还发现它不复制 msvcr100.dll 文件,但是我已经声明了两次!我已经手动将它复制到目标主机。

这里是pyftp1.py:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys, time, os, hashlib
import ftplib
import traceback
from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog  # <-- Line 7
from PyQt5.QtWidgets import QPushButton, QHBoxLayout, QVBoxLayout, QScrollArea, QLineEdit, QCheckBox
from PyQt5 import QtGui
from PyQt5.QtGui import QIcon, QPalette, QLinearGradient, QColor, QBrush
from PyQt5.QtCore import QObject, QThread, pyqtSignal, pyqtSlot

你可以试试这个:

  1. 将 'libEGL.dll' 添加到您的工作文件夹。这是来自Python3x\Lib\site-packages\PyQt5

  2. 在 cx-freeze setup.py 添加:

    如果sys.platform == "win32": includefiles = ['libEGL.dll'] #这里如果需要的话应该添加其他文件

  3. 在 cx-freeze setup.py link "includefiles" 到 build_exe_options 像这样:

    build_exe_options={ ... ... 'include_files': 包含文件, }

解决方案。

在为一个问题苦苦挣扎了很长时间之后,我找到了解决方案。许多指南不考虑特定版本的软件,或使用的某些特定模块,甚至他们不检查新系统上的 .exe,它没有您的库 dll 等。这是有效且经过测试的解决方案。

首先,我删除了cx_freeze并使用了py2exe。 我已经在 WinXP 32 位上构建了所​​有的东西——为了达到目标,它可以在几乎每个系统上 运行。 您需要:

  • Win XP 32 位(或您认为最低要求的其他系统)
  • Python 3.4 -- 从 www.python.ord/download 安装 Windows x86 msi 安装程序
  • PyWin32 -- 从 sf.net pywin32-219.win32.py3.4.exe
  • 安装
  • Qt5.5 -- 从 qt.io 安装它,你也会得到 mingw,接下来将文件夹 C:\Qt\Tools\mingw492_32\bin 添加到你的系统环境 %PATH%.
  • Git for Windows -- 从 git-scm.com
  • 安装
  • PyQt5 -- 从 riverbankcomputing.co.uk win32 x86 安装程序和 运行 下载它
  • SIP -- 最糟糕的安装!
    • 从 riverbankcomputing.co.uk、
    • 下载
    • 解压缩,
    • 运行 Git Bash,
    • python configure.py -p win32-g++ ,
    • mingw32-make.exe 安装。
    • 失败(。你手动去sipgen/文件夹,复制sip.exe到c:\Python34。接下来去../siplib,复制sip.pyd到c :\python34\Lib\site-packages,然后
    • strip /c/Python34/Lib/site-packages/sip.pyd
    • cp sip.h /c/Python34/include/
  • py2exe -- python -m pip install py2exe
  • 7-Zip -- 从 7-zip.org 安装为 x86 exe
  • 7-zip extra,解压到已安装的 7-zip 文件夹中,与 .txt 文件的冲突可以忽略
  • Resource Hacker -- 来自 angusj.com -- 但是如果你不想设置为你的 .exe 最后打补丁并给它一个漂亮的图标,就没有必要了

接下来我将setup.py添加到项目中:

from distutils.core import setup
import os, sys
import py2exe
from glob import glob
import PyQt5

NAME="ProgName"

qt_platform_plugins = [("platforms", glob(PyQt5.__path__[0] + r'\plugins\platforms\*.*'))]
data_files.extend(qt_platform_plugins)
msvc_dlls = [('.', glob(r'C:\Windows\System32\msvc?100.dll'))]
data_files.extend(msvc_dlls)
# print(data_files)

sys.argv.append('py2exe')

setup(
    data_files=data_files,
    # windows=["pyftp1.py",],
    windows=[
        {
            "script": "pyftp1.py",
            "icon_resources": [(0, "resources/favicon.ico")]
        }
    ],
    # zipfile=None,
    options={
        "py2exe": {
            "includes":["sip", "atexit",],
            # "packages": ['PyQt5'],
            "compressed": True,
            "dist_dir": "dist/" + NAME,
            # "bundle_files": 0,
            # "zipfile": None,
            "optimize": 2,
        }
    }
)

只是不要手动运行所有的动作,你可以添加Makefile

# start settings
DIST=dist
# change NAME also in setup.py and resource/config.txt
NAME=ProgName
EXT=exe

# final name and location of the built program
FINAL=$(DIST)/$(NAME).$(EXT)

# external programs
7ZIPDIR="C:\Program Files-Zip"
RESHACKER="/c/Program\ Files/Resource\ Hacker/ResourceHacker.exe"

# intermediate steps

# no icon version of program 
NOICON=$(DIST)/$(NAME)_no_icon.$(EXT)

# name of .7z archive
7Z_BASENAME=$(NAME).7z
7Z=$(DIST)/$(7Z_BASENAME)

# folder with ready .exe
PROGDIR=$(DIST)/$(NAME)

all: $(FINAL)


$(FINAL): $(NOICON)
    # change icon
    "$(RESHACKER)" -addoverwrite $(NOICON), $(FINAL), resources/favicon.ico, ICONGROUP, MAINICON, 0


$(NOICON): $(7Z)
    #build autorunning sfx with default icon
    cat $(7ZIPDIR)/7zS.sfx resources/config.txt $(7Z) > $(NOICON)


$(7Z): exe
    # compress program folder to .7z
    cd $(DIST);  $(7ZIPDIR)\z.exe a  $(7Z_BASENAME)  $(NAME)


exe:  $(DIST)
    # build program itself
    python setup.py py2exe


$(DIST):
    # create dist directory
    # echo $(DIST)/


clean:
    rm -rf $(DIST)/*

现在您可以轻松地构建您的程序本身 - 作为文件的文件夹: mingw32-make.exe exe 或者制作一个带有正确图标的文件: mingw32-make.exe全部 或者清理所有东西: mingw32-make.exe 干净