Importing a package and looping through modules in the package

Dave rezaim_1999 at yahoo.com
Mon Feb 9 22:29:27 EST 2004


Hi Everyone,

I am trying to import a package and then loop through the modules
inside the package, but I'm running to a problem.  Basically:
-----
I have a package called
regression.changepassword.unittests which contains test modules.
---
In a test agent module (called agent.a), I do the following:

from regression.changepassword.unittests import *
utilities.runTests("regression.changepassword.unittests.")
----
in the utilities.runTests, I do the following:
    for id in sys.modules.keys():
        if id.startswith(testtype) and sys.modules[id] != None:
            print "module id is: " + `id`
---------------------------------------------------
However, the loop is never executed since it seems the list of modules
in regression.changepassword.unittests is never added to the
sys.modules.  If I import the tests one by one as the follwing:

from regression.changepassword.unittests import CPWD_2
from regression.changepassword.unittests import CPWD_3

Then the program works and loops through the test modules correctly. 
Am I doing something wrong?  Or that is how Python works and I have to
import modules one by one.  I appreciate any help that you can offer
me and thanks for your time ( I apologize if my message is too long).

Thanks,
Bardia



More information about the Python-list mailing list