pydoc的locate模块无法正确处理错误

时间:2016-06-13 10:49:43

标签: python python-2.7 error-handling pydoc

从代码块或基本交互式shell调用时,

pydoc.locate不会抛出错误。只有当我从pycharm中启动IPython控制台时,如果提供不存在的模块作为参数,locate会抛出错误。

Python代码:打印None作为输出

import pydoc


def test_pydoc_locate():
    print pydoc.locate('nonexistent.module.name')

if __name__ == "__main__":
    test_pydoc_locate()

从pycharm运行IPython

from pydoc import locate
imported_module = locate('nonexistent.module.name')
Traceback (most recent call last):
  File "/home/testuser/anaconda/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 3035, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-3-756f8a48c1ad>", line 1, in <module>
    imported_module = locate('nonexistent.module.name')
  File "/home/testuser/anaconda/lib/python2.7/pydoc.py", line 1517, in locate
    nextmodule = safeimport(join(parts[:n+1], '.'), forceload)
  File "/home/testuser/anaconda/lib/python2.7/pydoc.py", line 342, in safeimport
    raise ErrorDuringImport(path, sys.exc_info())
ErrorDuringImport: problem in nonexistent - <type 'exceptions.ImportError'>: No module named nonexistent

从终端运行python

>>> from pydoc import locate
>>> imported_module = locate('nonexistent.module.name')
>>> print imported_module
None

pycharm IDE是否设置了一些系统变量,使locate能够意识到存在导入错误?

如果有人可以测试并解释这种行为,我将非常感激。

谢谢!

0 个答案:

没有答案