Question about packages

Mark McEahern mark at mceahern.com
Mon Dec 29 23:02:40 EST 2003


On Mon, 2003-12-29 at 21:44, djw wrote:
> So, the question is, how do you import into a sub-package to allow both 
> a script at the root level to import the module ("library mode") and for 
> the module itself to run in "test harness" mode with the same import?

You could do something like:

try:
    import X.modX1
except ImportError:
    if __name__ == '__main__':
        # Hmm, must be running the test harness, adjust
        # sys.path appropriately, then retry import
        import sys
        sys.path.insert(0, whatever)
        import X.modX1

but that smells.

Maybe it's best to separate tests from code?

Cheers,

// m






More information about the Python-list mailing list