使用 cx_freez 构建 exe 时出错
Posted
技术标签:
【中文标题】使用 cx_freez 构建 exe 时出错【英文标题】:Error during building of exe using cx_freez 【发布时间】:2019-01-03 10:51:54 【问题描述】:我得到:
"import error :No Module named 'PyQt5.QtGui'" during the build.
setup.py
文件和.py
的代码 sn-p 如下。
请查看并建议我需要对安装文件或我尝试生成 exe 的代码进行更改。
我正在使用winpython
的分布,因此使用了大的排除列表。
我看过使用 pyinstaller 但www.pyinstaller.org
似乎已关闭。如果有更简单的方法将我的代码转换为 exe ,请分享。
来自 .py 的代码 -
import sys
from PyQt5.QtWidgets import QApplication,QPushButton, QInputDialog,
QLineEdit, QFileDialog,QMainWindow
from PyQt5.QtGui import QIcon
import csv
import pathlib
import os
import pandas as pd
import gc
import numpy as np
import matplotlib
import xlsxwriter
设置文件-
import os.path
import sys
from cx_Freeze import setup, Executable
PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')
build_exe_options = "packages": ["os","pandas", "numpy","sys","PyQt5.QtWidgets","PyQt5.QtGui","csv","pathlib","gc","matplotlib","xlsxwriter"], "excludes": ["adodbapi","alabaster","algopy","altair-widgets","altair","astroid","astroml","babel","backports-abc","backports.weakref","baresql","bcolz","beautifulsoup4","bkcharts",
"blaze","bleach","blosc","bloscpack","bokeh","boto3","botocore","bottleneck","bqplot","brewer2mpl","brotli","certifi","cffi","chardet","click","cloudpickle","clrmagic","colorama",
"colorcet","coloredlogs","comtypes","cvxcanon","cvxopt","cvxpy","cx-freeze","cycler","cyordereddict","cython","cytoolz","dask","datashader","datashape","db.py","decorator","dill",
"distributed","docopt","docrepr","docutils","ecos","edward","emcee","entrypoints","enum34","fast-histogram","fastcache","fasteners","fastparquet","feather-format","flask-cors",
"flask","formlayout","fuzzywuzzy","geopy","gitdb2","gitpython","gmpy2","greenlet","guidata","guiqwt","h5py","heapdict","holoviews","html5lib","humanfriendly","husl","idna","imageio",
"imagesize","ipykernel","ipyleaflet","ipympl","ipyparallel","ipython-genutils","ipython-sql","ipython","ipywidgets","isort","itsdangerous","jedi","jinja2","jmespath","joblib",
"jsonschema","julia","jupyter-client","jupyter-console","jupyter-core","jupyter-sphinx","jupyter","jupyterlab-launcher","jupyterlab","keras-vis","keras","lasagne","lazy-object-proxy",
"llvmlite","lmfit","locket","lxml","markdown","markupsafe","mccabe","metakernel","mistune","mizani","mkl-service","monotonic","moviepy","mpl-scatter-density","mpld3","mpldatacursor",
"mpmath","msgpack-python","multipledispatch","multiprocess","mypy","mysql-connector-python","nbconvert-reportlab","nbconvert","nbdime","nbformat","netcdf4","networkx","nltk","notebook",
"numba","numdifftools","numexpr","numpydoc","oct2py","octave-kernel","odo","olefile","palettable","pandas-datareader","pandocfilters","param","partd","patsy","pdfrw","pep8","pexpect",
"pg8000","pickleshare","pillow","pip","pkginfo","plotnine","ply","prettytable","prompt-toolkit","protobuf","psutil","ptpython","ptyprocess","pulp","pweave",
"py","pyarrow","pyaudio","pybars3","pybind11","pycodestyle","pycparser","pyflakes","pyflux","pygame","pygments","pylint","pymc3","pymc","pymeta3","pymongo","pyodbc","pyopengl",
"pypandoc","pyparsing","pyqtgraph","pyreadline","pyserial","pystache","pytest","python-dateutil","python-hdf4","python-levenshtein","python-snappy","pythonnet","pythonqwt","pytz","pywavelets",
"pywin32","pywinpty","pywinusb","pyyaml","pyzmq","qtawesome","qtconsole","qtpy","redis","reportlab","requests-file","requests-ftp","requests-toolbelt","requests","rope",
"rpy2","rx","s3fs","s3transfer","scikit-fuzzy","scikit-image","scikit-learn","scikit-neuralnetwork","scikit-optimize","scilab2py","scipy","scs","seaborn","selenium","setuptools",
"simplegeneric","simplejson","sip","six","smmap2","snakeviz","snowballstemmer","sortedcontainers","sounddevice","sphinx-rtd-theme","sphinx","sphinxcontrib-websupport","spyder-notebook","spyder-reports",
"spyder-terminal","spyder","sqlalchemy","sqlite-bro","sqlparse","statsmodels","supersmoother","sympy","tables","tblib","tensorflow","testpath","theano","thriftpy","toolz","tornado","tqdm","traitlets","traittypes","twine",
"twitter","typed-ast","urllib3","vega","vitables","wcwidth","webencodings","werkzeug","wheel","widgetsnbextension","winpython","wordcloud","wrapt","xarray","xlrd","xlwings","zarr","zict"]
setup(
name="MyProgram",
version="0.1",
description="MyDescription",
options = "build_exe": build_exe_options,
executables=[Executable("pyqt5.py",
base = ("Win32GUI" if sys.platform == "win32"
else None))],
)
【问题讨论】:
你看过this question吗? 【参考方案1】:错误代码表明错误来自这一行
from PyQt5.QtGui import QIcon
你确定你的环境中安装了PyQT5.QtGui
【讨论】:
是的,我可以运行程序,但在构建过程中出现错误。 在你的程序中你导入PyQt5.QtGui
虽然以上是关于使用 cx_freez 构建 exe 时出错的主要内容,如果未能解决你的问题,请参考以下文章
使用 cx_freeze 将 .py 转换为 .exe 时出错
cx_Freeze - 从桌面快捷方式运行 .exe 时出错
使用 cx_Freeze 构建可执行文件时出错:IndexError: tuple index out of range