1

I have MBP13'R running the latest OSX.

For some reason I can't run doctest...

Python code:

def area_tri(base, height):
    """
    >>>area_tri(10, 10)
    50
    """
    return base / 2 * height

import doctest

doctest.testmod()

Error message:

  Traceback (most recent call last):
  File "/Users/name/Documents/doctest.py", line 8, in <module>
    import doctest
  File "/Users/name/Documents/doctest.py", line 9, in <module>
    doctest.testmod()
  AttributeError: 'module' object has no attribute 'testmod'

I only just started programming on my Mac and I can't fix it... Python version is 3.3.4

2
  • What's your python version? Commented Mar 13, 2014 at 9:52
  • Python version is 3.3.4 Commented Mar 13, 2014 at 9:55

1 Answer 1

4

Your file naming is confusing the interpreter. You've named your file "/Users/name/Documents/doctest.py" which has the same name as the doctest module.

Change the name and try again (make sure you remove the old doctest.pyc as well).

Demo:

Your code breaks as expected:

msvalkon@Lunkwill:/tmp$ python doctest.py 
Traceback (most recent call last):
  File "doctest.py", line 8, in <module>
    import doctest
  File "/tmp/doctest.py", line 9, in <module>
    doctest.testmod()
AttributeError: 'module' object has no attribute 'testmod'

After renaming and removing the .pyc

msvalkon@Lunkwill:/tmp$ mv doctest.py dtest.py 
msvalkon@Lunkwill:/tmp$ rm doctest.pyc
msvalkon@Lunkwill:/tmp$ python dtest.py
Traceback (most recent call last):
  File "dtest.py", line 9, in <module>
    doctest.testmod()
  File "/usr/lib/python2.7/doctest.py", line 1885, in testmod
    for test in finder.find(m, name, globs=globs, extraglobs=extraglobs):
  File "/usr/lib/python2.7/doctest.py", line 900, in find
    self._find(tests, obj, name, module, source_lines, globs, {})
  File "/usr/lib/python2.7/doctest.py", line 954, in _find
    globs, seen)
  File "/usr/lib/python2.7/doctest.py", line 942, in _find
    test = self._get_test(obj, name, module, globs, source_lines)
  File "/usr/lib/python2.7/doctest.py", line 1026, in _get_test
    filename, lineno)
  File "/usr/lib/python2.7/doctest.py", line 645, in get_doctest
    return DocTest(self.get_examples(string, name), globs,
  File "/usr/lib/python2.7/doctest.py", line 659, in get_examples
    return [x for x in self.parse(string, name)
  File "/usr/lib/python2.7/doctest.py", line 621, in parse
    self._parse_example(m, name, lineno)
  File "/usr/lib/python2.7/doctest.py", line 679, in _parse_example
    self._check_prompt_blank(source_lines, indent, name, lineno)
  File "/usr/lib/python2.7/doctest.py", line 766, in _check_prompt_blank
    line[indent:indent+3], line))
ValueError: line 2 of the docstring for __main__.area_tri lacks blank after >>>: '>>>area_tri(10, 10)'

Notice that you are not following doctest rules but this is another problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.