【问题标题】:AttributeError: 'module' object has no attribute 'testmod' Python doctestAttributeError: 'module' 对象没有属性 'testmod' Python doctest
【发布时间】:2015-05-19 07:06:35
【问题描述】:

每当我尝试在 python 中进行 doctest 时,基本上每当我运行代码时

if __name__ =="__main__":
   import doctest
   doctest.testmod()

我从口译员那里得到这个回复

AttributeError: 'module' 对象没有属性 'testmod'

我可以很好地运行这段代码,但是每当我在我的 Windows 机器上运行它时,它就不起作用了。

我的机器运行的是 Windows,他们的机器是 OS X,但运行的是 python 2.7.5。

谢谢你:)

【问题讨论】:

    标签: windows macos python-2.7 doctest


    【解决方案1】:

    确保您没有尝试将测试文件保存为doctest.py。上面建议的打印语句将显示它。如果文件名为doctest.py,则重命名并重试。

    【讨论】:

      【解决方案2】:

      AttributeError: 'module' 对象没有属性 'testmod'

      显然您正在导入的 doctest 模块没有 testmod() 方法。

      可能的原因有:

      • lib 中有多个 doctest 模块。
      • 并且,由于import doctest 而被导入的是另一个(没有testmod() 方法)。

      解决方法:寻找标准doctest模块的路径。

      if __name__ =="__main__":
         import doctest
         if doctest.__file__  == "/path/to/standard/doctest-module":
             doctest.testmod()
      

      【讨论】:

        【解决方案3】:

        看起来有一个名为doctest 的不同模块正在被导入,而不是标准模块。

        要找出准确导入的模块,只需添加以下print

        if __name__ =="__main__":
           import doctest
           print doctest.__file__  # add this
           doctest.testmod()
        

        print 应该产生类似于C:\Python27\lib\doctest.pyc 的内容,具体取决于您使用的 Python 的位置和版本。任何其他输出都意味着您正在导入错误的模块,并解释您收到错误的原因。

        【讨论】:

          猜你喜欢
          • 2021-01-15
          • 2015-05-16
          • 1970-01-01
          • 2022-12-20
          • 2014-04-03
          • 2016-01-18
          • 1970-01-01
          • 2018-01-14
          • 2014-08-15
          相关资源
          最近更新 更多