Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 335055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:06:47+00:00 2026-05-12T10:06:47+00:00

I’ve got a fairly basic doctestable file: class Foo(): >>> 3+2 5 if __name__

  • 0

I’ve got a fairly basic doctestable file:

class Foo():
    """
    >>> 3+2
    5
    """

if __name__ in ("__main__", "__console__"):
    import doctest
    doctest.testmod(verbose=True)

which works as expected when run directly through python.

However, in iPython, I get

1 items had no tests:
    __main__
0 tests in 1 items.
0 passed and 0 failed.
Test passed.

Since this is part of a Django project and will need access to all of the appropriate variables and such that manage.py sets up, I can also run it through a modified command, which uses code.InteractiveConsole, one result of which is __name__ gets set to ‘__console__‘.

With the code above, I get the same result as with iPython. I tried changing the last line to this:

 this = __import__(__name__)
 doctest.testmod(this, verbose=True)

and I get an ImportError on __console__, which makes sense, I guess. This has no effect on either python or ipython.

So, I’d like to be able to run doctests successfully through all three of these methods, especially the InteractiveConsole one, since I expect to be needing Django pony magic fairly soon.

Just for clarification, this is what I’m expecting:

Trying:
    3+2
Expecting:
    5
ok
1 items had no tests:
    __main__
1 items passed all tests:
   1 tests in __main__.Foo
1 tests in 2 items.
1 passed and 0 failed.
Test passed.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T10:06:47+00:00Added an answer on May 12, 2026 at 10:06 am

    The following works:

    $ ipython
    ...
    In [1]: %run file.py
    
    Trying:
        3+2
    Expecting:
        5
    ok
    1 items had no tests:
        __main__
    1 items passed all tests:
       1 tests in __main__.Foo
    1 tests in 2 items.
    1 passed and 0 failed.
    Test passed.
    
    In [2]: 
    

    I have no idea why ipython file.py does not work. But the above is at least a workaround.

    EDIT:

    I found the reason why it does not work. It is quite simple:

    • If you do not specify the module to test in doctest.testmod(), it assumes that you want to test the __main__ module.
    • When IPython executes the file passed to it on the command line, the __main__ module is IPython’s __main__, not your module. So doctest tries to execute doctests in IPython’s entry script.

    The following works, but feels a bit weird:

    if __name__ == '__main__':
        import doctest
        import the_current_module
        doctest.testmod(the_current_module)
    

    So basically the module imports itself (that’s the “feels a bit weird” part). But it works. Something I do not like abt. this approach is that every module needs to include its own name in the source.

    EDIT 2:

    The following script, ipython_doctest, makes ipython behave the way you want:

    #! /usr/bin/env bash
    
    echo "__IP.magic_run(\"$1\")" > __ipython_run.py
    ipython __ipython_run.py
    

    The script creates a python script that will execute %run argname in IPython.

    Example:

    $ ./ipython_doctest file.py
    Trying:
        3+2
    Expecting:
        5
    ok
    1 items had no tests:
        __main__
    1 items passed all tests:
       1 tests in __main__.Foo
    1 tests in 2 items.
    1 passed and 0 failed.
    Test passed.
    Python 2.5 (r25:51908, Mar  7 2008, 03:27:42) 
    Type "copyright", "credits" or "license" for more information.
    
    IPython 0.9.1 -- An enhanced Interactive Python.
    ?         -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help      -> Python's own help system.
    object?   -> Details about 'object'. ?object also works, ?? prints more.
    
    In [1]:
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 214k
  • Answers 215k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'm skeptical that all Android devices would support the exact… May 12, 2026 at 10:52 pm
  • Editorial Team
    Editorial Team added an answer Single opcodes are thread-safe because of the GIL but nothing… May 12, 2026 at 10:52 pm
  • Editorial Team
    Editorial Team added an answer ChoiceField is not really suitable for multiple choices, instead I… May 12, 2026 at 10:52 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS
I want use html5's new tag to play a wav file (currently only supported

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.