Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 :)

like image 808
Johnny Mann Avatar asked Mar 17 '15 00:03

Johnny Mann


3 Answers

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.

like image 152
Andrei K. Avatar answered Nov 19 '22 19:11

Andrei K.


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()
like image 20
Nabeel Ahmed Avatar answered Nov 19 '22 18:11

Nabeel Ahmed


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.

like image 29
asherbret Avatar answered Nov 19 '22 18:11

asherbret