tags:

views:

191

answers:

2

I try to use doctest from example from http://docs.python.org/library/doctest.html

But when I run

python example.py -v

I get this

Traceback (most recent call last):
  File "example.py", line 61, in <module>
    doctest.testmod()
AttributeError: 'module' object has no attribute 'testmod'

But I can import doctest in python interactive shell and enable to use doctest.testmod() as well. I searched in google and didn't find the solution.

Python version is 2.5.1 on Max OSX

A: 

Try inserting a

print doctest, dir(doctest)

before line 61. This will tell you the location of the doctest module, and what attributes it has. You can do this to make sure that there's nothing wrong with your doctest module.

Unknown
+2  A: 

Clearly the doctest module object you have at hand at that point is NOT the normal, unadulterated one you get from an import doctest from the standard library. Printing doctest.__file__ (and sys.stdout.flush()ing after that, just to make sure you do get to see the results;-) before the line-61 exception will let you know WHERE that stray doctest module is coming from.

If you show us example.py as well as that output we can probably point out what exactly you may be doing wrong, if it doesn't already become obvious to you.

Alex Martelli
Very thanks! It's because that I named the script as doctest.py before, and it doesn't work so I changed to example.py. But doctest.pyc is still there then doctest.pyc is imported instead.
noomz
Python will recompile the pyc if that's required and possible, but, yes, if it has the pyc and no source for it, then it will just use the existing pyc file.
Alex Martelli