我正在使用python 3.6,cx_Freeze 5.1.1。
我已经制作了一个使用此模块制作wordcloud的代码:
我将它放在PyQt5 UI中,如下面的代码所示:
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QDialog, QApplication, QMainWindow
from PyQt5 import QtCore, QtGui, QtWidgets
import numpy as np
from PIL import Image
from os import path
from wordcloud import WordCloud
class AppWindow(QDialog):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.show()
class Ui_MainWindow(object):
currdir = 'C:\\Users\\lotem\\Desktop'
def create_wordcloud(text):
mask = np.array(Image.open(path.join(currdir, "cloud.png")))
wc = WordCloud(background_color="white",
mask=mask,
max_words=200)
wc.generate(text)
wc.to_file(path.join(currdir, "wc.png"))
def MakeWordCloud(self):
text = self.textEdit.toPlainText()
create_wordcloud(text)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(904, 675)
MainWindow.setStyleSheet("background-color: rgb(217, 14, 85);")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setStyleSheet("background-color: rgb(255, 255, 255);")
self.textEdit.setObjectName("textEdit")
self.gridLayout.addWidget(self.textEdit, 1, 0, 1, 3)
self.label = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setFamily("Assistant Light")
font.setPointSize(25)
font.setBold(False)
font.setWeight(50)
self.label.setFont(font)
self.label.setStyleSheet("color: rgb(255, 255, 255);")
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 1, 1, 1)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setFamily("Assistant Light")
font.setPointSize(11)
self.pushButton.setFont(font)
self.pushButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.pushButton.setStyleSheet("background-color: rgb(255, 85, 127);\n"
"color: rgb(255, 255, 255);")
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 3, 2, 1, 1)
### Create word cloud button
self.pushButton.clicked.connect(self.MakeWordCloud)
MainWindow.setCentralWidget(self.centralwidget)
self.menuBar = QtWidgets.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 904, 31))
self.menuBar.setObjectName("menuBar")
self.menu = QtWidgets.QMenu(self.menuBar)
self.menu.setObjectName("menu")
MainWindow.setMenuBar(self.menuBar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "word cloud"))
self.textEdit.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">type text</p></body></html>"))
self.label.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\">word cloud</p></body></html>"))
self.pushButton.setText(_translate("MainWindow", "create word cloud"))
app = QApplication(sys.argv)
window = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(window)
window.setWindowIcon(QIcon('cloudicon.png'))
window.show()
sys.exit(app.exec_())
这个程序基本上有一个文本框和一个按钮,当你点击它时,它会使用上面的模块创建wordcloud。
当我从PyCharm运行程序时,程序运行正常。
我已经使用cx_Freeze将其作为.exe程序。当我单击按钮在.exe程序中创建wordcloud时,它将停止工作。
我认为cx_Freeze不会导入WordCloud模块。我已手动将其添加到&#39;包含&#39;在我的setup.py文件中,甚至在&#39; include_files&#39;中单独添加了模块中的每个文件。两者都没有用。
setup.py代码:
from cx_Freeze import setup, Executable
import sys
base = None
if (sys.platform == "win32"):
base = "Win32GUI"
additional_mods = ['numpy.core._methods', 'numpy.lib.format']
executables = [Executable("test.py", base=base)]
packages = ["idna", 'arabic_reshaper', 'pkg_resources', 'wordcloud', 'stop_words', 'numpy', 'bidi.algorithm', 'WordCloud']
options = {
'build_exe': {
'packages':packages,
'includes':['numpy.core._methods', 'numpy.lib.format', 'arabic_reshaper', 'pkg_resources', 'wordcloud', 'WordCloud' ,
'stop_words', 'numpy', 'bidi.algorithm'],
'include_files':[('C:\\Users\\lotem\\Desktop\\WordCloud\\cloud.png'),
('C:\\Users\\lotem\\Desktop\\WordCloud\\Shoroq-Font.ttf'),
('C:\\Users\\lotem\\Desktop\\WordCloud\\NewMainWin.py'),
('C:\\Users\\lotem\\Desktop\\WordCloud\\WordCloudCode.py'),
('C:\\Users\\lotem\\Desktop\\WordCloud\\cloudicon.png'),
('C:\\Users\\lotem\\Desktop\\WordCloud\\venv\\Lib\\site-packages\\wordcloud\\__init__.py'),
('C:\\Users\\lotem\\Desktop\\WordCloud\\venv\\Lib\\site-packages\\wordcloud\\color_from_image.py'),
('C:\\Users\\lotem\\Desktop\\WordCloud\\venv\\Lib\\site-packages\\wordcloud\\DroidSansMono.ttf'),
('C:\\Users\\lotem\\Desktop\\WordCloud\\venv\\Lib\\site-packages\\wordcloud\\query_integral_image.cp36-win32.pyd'),
('C:\\Users\\lotem\\Desktop\\WordCloud\\venv\\Lib\\site-packages\\wordcloud\\stopwords'),
('C:\\Users\\lotem\\Desktop\\WordCloud\\venv\\Lib\\site-packages\\wordcloud\\tokenization.py'),
('C:\\Users\\lotem\\Desktop\\WordCloud\\venv\\Lib\\site-packages\\wordcloud\\wordcloud.py'),
('C:\\Users\\lotem\\Desktop\\WordCloud\\venv\\Lib\\site-packages\\wordcloud\\wordcloud_cli.py'),
('C:\\Users\\lotem\\Desktop\\WordCloud\\venv\\Lib\\site-packages\\wordcloud\\__pycache__\\__init__.cpython-36.pyc'),
('C:\\Users\\lotem\\Desktop\\WordCloud\\venv\\Lib\\site-packages\\wordcloud\\__pycache__\\color_from_image.cpython-36.pyc'),
('C:\\Users\\lotem\\Desktop\\WordCloud\\venv\\Lib\\site-packages\\wordcloud\\__pycache__\\tokenization.cpython-36.pyc'),
('C:\\Users\\lotem\\Desktop\\WordCloud\\venv\\Lib\\site-packages\\wordcloud\\__pycache__\\wordcloud.cpython-36.pyc'),
('C:\\Users\\lotem\\Desktop\\WordCloud\\venv\\Lib\\site-packages\\wordcloud\\__pycache__\\wordcloud_cli.cpython-36.pyc')
]
}
}
setup(
name = "WordCloud",
options = options,
version = "1.0",
description = 'Wordcloud',
executables = executables
)
如何在我的代码中使用cx_Freeze导入模块?
由于