模块名称与程序包名称相同时,为什么doctest不起作用?

时间:2018-12-29 10:55:19

标签: python python-3.x doctest

此问题中的所有shell命令都将从foo目录的父目录中运行。

这是我的软件包目录foo的样子:

$ ls foo
__init__.py     bar.py          foo.py

以下是每个文件的内容:

$ cat foo/__init__.py 
"""Pacakge foo."""
$ cat foo/foo.py
"""Foo module

Example:

>>> from foo import foo
>>> foo.main()
hello, world
"""

def main():
    print('hello, world')
$ cat foo/bar.py
cat foo/bar.py
"""Foo module

Example:

>>> from foo import bar
>>> bar.main()
hello, world
"""

def main():
    print('hello, world')

这是我面临的问题:

$ python3 -m doctest foo/bar.py
$ python3 -m doctest foo/foo.py
**********************************************************************
File "foo/foo.py", line 5, in foo
Failed example:
    from foo import foo
Exception raised:
    Traceback (most recent call last):
      File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/doctest.py", line 1329, in __run
        compileflags, 1), test.globs)
      File "<doctest foo[0]>", line 1, in <module>
        from foo import foo
    ImportError: cannot import name 'foo' from 'foo' (foo/foo.py)
**********************************************************************
File "foo/foo.py", line 6, in foo
Failed example:
    foo.main()
Exception raised:
    Traceback (most recent call last):
      File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/doctest.py", line 1329, in __run
        compileflags, 1), test.globs)
      File "<doctest foo[1]>", line 1, in <module>
        foo.main()
    NameError: name 'foo' is not defined
**********************************************************************
1 items had failures:
   2 of   2 in foo
***Test Failed*** 2 failures.

为什么foo/foo.py的doctest失败?

0 个答案:

没有答案
相关问题