我正在使用 cx_freeze 使用 pyqt5 构建 py 文件的可执行文件。以下是错误
i am using cx_freeze to build a executable of py file with pyqt5. Following is the error
我是软件开发的新手,我正在使用 cx_freeze 制作一个使用 PyQt5 模块的 hello.py python 文件的可执行文件。重复出现以下错误。我按照说明使用 python 3.6 和 PyQt5。
AttributeError: 模块 'lxml.etrr' 没有属性 'fromstring'
下面是我的hello.py文件
import sys
import time
import wikipedia
import docx
import requests
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
doc = docx.Document()
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'Articulate - tecbeast.com'
self.left = 50
self.top = 100
self.width = 400
self.height = 300
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
lbl1 = QLabel(self)
lbl1.setText("Please enter the topics you want to print articles on")
lbl1.setGeometry(20,20,400,40)
lbl2 = QLabel(self)
lbl2.setText('Topics should be separeted by comma ","')
lbl2.setGeometry(20,40,400,40)
lbl3 = QLabel(self)
lbl3.setText('Make sure that your pc is connected to the internet')
lbl3.setGeometry(20,60,400,40)
# Create textbox
self.textbox = QLineEdit(self)
self.textbox.move(20, 100)
self.textbox.resize(280,40)
# Create a button in the window
self.button = QPushButton('Make Article', self)
self.button.move(20,160)
# connect button to function on_click
self.button.clicked.connect(self.on_click)
self.show()
def shw_wt(self):
#for the showing of label Please wait
lbl4 = QLabel(self)
lbl4.setText('Please wait')
lbl4.setGeometry(20,135,400,40)
lbl4.show()
@pyqtSlot()
def on_click(self):
self.shw_wt()
cur_topic = self.textbox.text()
topics = cur_topic.split(",")
for ech_topic in topics:
try:
para = wikipedia.summary(ech_topic, sentences = 100)
except wikipedia.exceptions.DisambiguationError as e:
para = wikipedia.summary(e.options[0], sentences = 100)
except requests.exceptions.ConnectionError as e:
pop_up = QMessageBox.question(self, 'Warning!', 'You are not connected to the internet', QMessageBox.Ok)
if pop_up == QMessageBox.Ok:
self.initUI()
#problem in above line
else:
pass
doc.add_heading (ech_topic,0)
doc.add_paragraph(para)
n = str(time.time())
doc.save(n+".docx")
pop_up = QMessageBox.question(self, 'All Done', 'Your article is made. Check your current directory', QMessageBox.Ok)
if pop_up == QMessageBox.Ok:
pass
else:
pass
#lbl4.hide()
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
您的 hello.py 有导入的库吗?
在您的 setup.py 中,您需要包含您使用过的模块并将它们包含在构建中。
这是一个 link 的答案,比我能解释得更好。根据您的回答,您将需要为您导入的所有模块使用路径。最好只使用每个模块所需的内容,并且可能需要对每个模块的使用方式进行更多研究。
在我的环境中重现了这个问题。
请试试我的setup.py
import sys
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {
"packages": ["idna","lxml"],
"includes": ["urllib3","queue"],
"excludes": ["tkinter"],
# "include_files":[('./platforms','./platforms')] # uncomment if need qwindows.dll for qt5 application, you need put qwindows.dll in "./platforms" folder.
}
# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup( name = "demo",
version = "0.1",
description = "demo",
options = {"build_exe": build_exe_options},
executables = [Executable("demo.py", base=base)])
根本原因 缺少目标文件夹中的 lxml
包。
您需要在下面的构建选项中指定包。
build_exe_options = {
"packages": ["idna","lxml"],
"includes": ["urllib3","queue"],
"excludes": ["tkinter"],
# "include_files":[('./platforms','./platforms')] # uncomment if need qwindows.dll for qt5 application, you need put qwindows.dll in "./platforms" folder.
}
我是软件开发的新手,我正在使用 cx_freeze 制作一个使用 PyQt5 模块的 hello.py python 文件的可执行文件。重复出现以下错误。我按照说明使用 python 3.6 和 PyQt5。
AttributeError: 模块 'lxml.etrr' 没有属性 'fromstring'
下面是我的hello.py文件
import sys
import time
import wikipedia
import docx
import requests
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
doc = docx.Document()
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'Articulate - tecbeast.com'
self.left = 50
self.top = 100
self.width = 400
self.height = 300
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
lbl1 = QLabel(self)
lbl1.setText("Please enter the topics you want to print articles on")
lbl1.setGeometry(20,20,400,40)
lbl2 = QLabel(self)
lbl2.setText('Topics should be separeted by comma ","')
lbl2.setGeometry(20,40,400,40)
lbl3 = QLabel(self)
lbl3.setText('Make sure that your pc is connected to the internet')
lbl3.setGeometry(20,60,400,40)
# Create textbox
self.textbox = QLineEdit(self)
self.textbox.move(20, 100)
self.textbox.resize(280,40)
# Create a button in the window
self.button = QPushButton('Make Article', self)
self.button.move(20,160)
# connect button to function on_click
self.button.clicked.connect(self.on_click)
self.show()
def shw_wt(self):
#for the showing of label Please wait
lbl4 = QLabel(self)
lbl4.setText('Please wait')
lbl4.setGeometry(20,135,400,40)
lbl4.show()
@pyqtSlot()
def on_click(self):
self.shw_wt()
cur_topic = self.textbox.text()
topics = cur_topic.split(",")
for ech_topic in topics:
try:
para = wikipedia.summary(ech_topic, sentences = 100)
except wikipedia.exceptions.DisambiguationError as e:
para = wikipedia.summary(e.options[0], sentences = 100)
except requests.exceptions.ConnectionError as e:
pop_up = QMessageBox.question(self, 'Warning!', 'You are not connected to the internet', QMessageBox.Ok)
if pop_up == QMessageBox.Ok:
self.initUI()
#problem in above line
else:
pass
doc.add_heading (ech_topic,0)
doc.add_paragraph(para)
n = str(time.time())
doc.save(n+".docx")
pop_up = QMessageBox.question(self, 'All Done', 'Your article is made. Check your current directory', QMessageBox.Ok)
if pop_up == QMessageBox.Ok:
pass
else:
pass
#lbl4.hide()
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
您的 hello.py 有导入的库吗?
在您的 setup.py 中,您需要包含您使用过的模块并将它们包含在构建中。
这是一个 link 的答案,比我能解释得更好。根据您的回答,您将需要为您导入的所有模块使用路径。最好只使用每个模块所需的内容,并且可能需要对每个模块的使用方式进行更多研究。
在我的环境中重现了这个问题。
请试试我的setup.py
import sys
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {
"packages": ["idna","lxml"],
"includes": ["urllib3","queue"],
"excludes": ["tkinter"],
# "include_files":[('./platforms','./platforms')] # uncomment if need qwindows.dll for qt5 application, you need put qwindows.dll in "./platforms" folder.
}
# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup( name = "demo",
version = "0.1",
description = "demo",
options = {"build_exe": build_exe_options},
executables = [Executable("demo.py", base=base)])
根本原因 缺少目标文件夹中的 lxml
包。
您需要在下面的构建选项中指定包。
build_exe_options = {
"packages": ["idna","lxml"],
"includes": ["urllib3","queue"],
"excludes": ["tkinter"],
# "include_files":[('./platforms','./platforms')] # uncomment if need qwindows.dll for qt5 application, you need put qwindows.dll in "./platforms" folder.
}