0

多年来,我的代码中已经导入了以下内容:

from etree.ElementTree import fromstring, parse, VERSION

今天我在移动(在 Eclipse/pyDev 中)一些不相关的源文件到另一个文件夹时犯了一个错误。该文件夹不是一个包,它花费了我一些清理、重建和 del *.pyc-s 才能再次找到它们。该部分已解决,但现在,上面的导入与“未解决的导入...”中断。当我删除 etree-prefix 时,导入已解决,但在运行时我得到

from ElementTree import fromstring, parse, VERSION
File "C:\Program Files\Python\EPD-7.3-2x64\Lib\xml\etree\ElementTree.py", line 127, in <module>
from . import ElementPath
ValueError: Attempted relative import in non-package

怎么了..?

4

1 回答 1

1

你应该做不到。

导入通常是from xml.etree.ElementTree import ...; 顶级包名称是xml.etree,而不是etree

看起来好像您已将xml.etree包添加到 Pythonsys.path模块搜索路径。不要那样做。从C:\Program Files\Python\EPD-7.3-2x64\Lib\xml\etree您的sys.path(或PYTHONPATH环境变量)中删除并从正确的顶级包名称中导入。

于 2014-03-11T10:50:51.513 回答