出于某种奇怪的原因,如果发生异常,我的Python解释器会执行文件./xml.py。我完全不知道为什么会这样 - 我绝对不希望这种情况发生。
我已经设置了一个名为xml.py的单个文件的新目录,其中只包含'print(“Hello xml.py”)行'。启动python3后,我在解释器的提示符下输入“a”。这应该显示一个简单的NameError,因为它当然没有定义,而是打印“Hello xml.py”(即执行./xml.py)和一堆其他错误,然后才将NameError转储到屏幕:
$ python3
Python 3.4.3 (default, Mar 26 2015, 22:03:40)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a
Hello xml.py
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
Error in sys.excepthook:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 15, in <module>
import xml.dom, xml.dom.minidom
ImportError: No module named 'xml.dom'; 'xml' is not a package
Original exception was:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
删除xml.py后,一切都恢复正常:
$ rm xml.py
$ python3
Python 3.4.3 (default, Mar 26 2015, 22:03:40)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>>
简单而简短的问题:这到底发生了什么?
答案 0 :(得分:0)
这些是线索:
Error in sys.excepthook:
...
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 15, in <module>
import xml.dom, xml.dom.minidom
ImportError: No module named 'xml.dom'; 'xml' is not a package
apport
是part of Ubuntu,无论好坏,似乎都试图捕获所有Python异常(使用sys.excepthook
)并处理它们。这样做时,它会尝试导入xml
包(part of the Python standard library)。因为您已将该包与本地文件别名,所以它会导入该文件,但找不到它所期望的包,并抛出另一个异常。
您可以将xml.py
重命名为不对标准库中的包进行别名的内容,或者在程序执行的早期将此行重命名为禁用apport
的异常挂钩:
# reassign sys.excepthook to its initial value
sys.excepthook = sys.__excepthook__
答案 1 :(得分:0)
Apport
是Ubuntu附带的程序。该程序的主要目的是检测程序崩溃并报告它们。每当发生异常时,Apport都会尝试使用异常挂钩来处理它。这里有关于此主题的错误报告:https://bugs.launchpad.net/ubuntu/+source/apport/+bug/109955
现在发生这种情况:当您尝试在控制台中键入a
时,会发生此操作:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
Apport有一个异常挂钩集,可以在此异常上调用。 Apport继续尝试报告它。在此过程中,它会尝试从标准库中导入名为xml
的包。但是,您将文件命名为xml.py
,因此它会隐藏标准库中的xml
包。因此导入失败:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 15, in <module>
import xml.dom, xml.dom.minidom
ImportError: No module named 'xml.dom'; 'xml' is not a package
请注意,xml
是标准库中的一个包(例如,它有一个__init__.py
)。但是,您创建了一个模块(xml.py
文件)。因此无法从中导入xml.dom
。
删除xml.py
后工作正常,因为工作目录中不再有一个模块影响标准目录中的模块。
禁用Apport :如果apport异常处理使您烦恼,可以在此主题后禁用它:https://askubuntu.com/questions/93457/how-do-i-enable-or-disable-apport - 但是,这会禁用一般的错误报告,我不会这样做。推荐。