8

When ever I try to doctest in python, basically whenever I run the code

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

I get this response from the interpreter

AttributeError: 'module' object has no attribute 'testmod'

I can run this code just fine, but whenever I run it on my windows machine, it doesn't work.

My machine is running Windows theirs is OS X, but are running python 2.7.5.

Thank you :)

Johnny Mann
  • 81
  • 1
  • 3

3 Answers3

11

Make sure that you are not trying to save your test file as doctest.py. The print statement suggested above will show it. If the file name is doctest.py, then rename it and try again.

Andrei K.
  • 191
  • 1
  • 7
4

AttributeError: 'module' object has no attribute 'testmod'

Clearly stats that the doctest module you're importing do not has the testmod() method.

Possible reasons can be:

  • You have more than one doctest modules in the lib.
  • and, it is the other one (without the testmod() method) which is getting imported as result of import doctest.

Solution: Look for the path of standard doctest module.

if __name__ =="__main__":
   import doctest
   if doctest.__file__  == "/path/to/standard/doctest-module":
       doctest.testmod()
Nabeel Ahmed
  • 18,328
  • 4
  • 58
  • 63
2

It looks like there is a different module called doctest that is being imported instead of the standard one.

To find out which module is being imported exactly, simply add the following print:

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

The print should produce something similar to C:\Python27\lib\doctest.pyc, depending on the location and version of Python you're using. Any other output means you are importing the wrong module, and explain why you're getting the error.

asherbret
  • 5,439
  • 4
  • 38
  • 58