为什么我得到这个ImportError?

时间:2014-03-02 20:51:22

标签: python lxml py2exe importerror lxml.html

我有tkinter个应用,我通过.exe汇总到py2exe

在设置文件中,我将其设置为包含lxmlurlliblxml.htmlastmath

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

当我运行.exe时,我会看到此弹出窗口。this http://gyazo.com/02fc6000c94c165a696d5072738c676d.png

然后我继续打开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文件的副本。

请帮我在这里找到问题。提前谢谢。

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:1)

Py2exe在此页面上记录了此错误:http://www.py2exe.org/index.cgi/WorkingWithVariousPackagesAndModules

他们也提供了一个有效的解决方案。