Making module content available in parent module
steve+comp.lang.python at pearwood.info
Tue Nov 23 13:35:02 CET 2010
On Tue, 23 Nov 2010 11:36:05 +0100, Ulrich Eckhardt wrote:
> foo.py # defines TestFoo1 and TestFoo2
> bar.py # defines TestBar1 and TestBar2
> What I would like to do now is this:
> from tests import *
> In other words, import all test files and run them. This does import
> them, but it turns out that I end up with modules foo and bar, and the
> unittests inside those are not found.
Given the directory structure you show, I find that hard to believe. You
should get an ImportError, as there is no module or package called
But suppose you turn tests into a proper package:
You could have __init__.py include these lines:
from foo import *
from bar import *
Then later, when you do this:
from tests import *
it will pick up everything from foo and bar, and unittest.main() should
run those tests as well. I think.
Or you could just do:
for module in (foo, bar):
> PS: I've been trying a few things here, and stumbled across another
> thing that could provide a solution. I can "from tests import *", but
> then all these modules will pollute my namespace. I can "import tests",
> but then neither of the submodules will be in "tests". I tried "import
> tests.*", but that doesn't work. Is there no way to import a whole
> package but with its namespace?
The package needs to know what submodules to make available. Put inside
and then from outside the package, do this:
Now tests.foo and tests.bar will exist.
More information about the Python-list