python - 为什么我会收到此 ImportError?

标签 python lxml py2exe importerror lxml.html

我有一个 tkinter 应用程序,我正在通过 py2exe 编译成一个 .exe

在设置文件中,我设置了包含lxmlurlliblxml.htmlast数学

当我在 CMD 控制台中运行 python setup.py py2exe 时,编译正常。然后我转到它创建的 dist 文件夹,并运行 .exe 文件。

当我运行 .exe 时,我得到了这个弹出窗口。 this
(来源:gyazo.com)

然后我继续打开 Trader.exe.log 文件,内容如下;

Traceback (most recent call last):
  File "Trader.py", line 1, in <module>
  File "lxml\html\__init__.pyc", line 42, in <module>
  File "lxml\etree.pyc", line 12, in <module>
  File "lxml\etree.pyc", line 10, in __load
  File "lxml.etree.pyx", line 84, in init lxml.etree (src\lxml\lxml.etree.c:190292)
ImportError: cannot import name _elementpath

包括here是我的 setup.py 文件的副本。

请帮我找出问题所在。提前致谢。

最佳答案

看起来 py2exe 没有意识到它应该包含 lxml._elementpath 模块,该模块由 lxml.etree 有条件地导入。您可以告诉它在您的 setup.py 中使用 includes 关键字参数显式包含该模块。

setup(
    options={'py2exe': {"includes": ["lxml._elementpath"]}}
)

关于python - 为什么我会收到此 ImportError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22133313/

相关文章:

python - 在 python 中使用 urllib 时双引号和单引号字符串之间的不同行为

python - 如何在 python、odoo 中使用 For 循环正确计数?

python 如何获取导入包中运行脚本的文件名

python - 当我使用 lxml 获取网站信息时,为什么程序返回空列表

python - 从表中提取行

xml - 无法在 xml 中找到具有命名空间的元素

python - 将 bundle_files = 1 与 py2exe 一起使用不起作用

python - 在循环中绘制子图

python - shutil.copyfile 的替代品

python - 使用#-*-编码: utf-8 -*- does not remove "Non-ASCII character '\x9 0' in file hello.exe on line 1, but no encoding declared" error