【问题标题】:can no longer import from ElementTree无法再从 ElementTree 导入
【发布时间】:2014-03-11 10:47:54
【问题描述】:

多年来,我的代码中有以下导入:

from etree.ElementTree import fromstring, parse, VERSION

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

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

出了什么问题..?

【问题讨论】:

    标签: import pydev elementtree python-import xml.etree


    【解决方案1】:

    你应该做不到。

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

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

    【讨论】:

    • pyDev 仍然将其标记为未解决,但运行时的 Python 对此没有任何问题。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2022-07-07
    • 2018-02-08
    • 2014-09-14
    • 2020-04-07
    • 2023-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多