Importing a package and looping through modules in the package

D

Dave

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
 
R

Rich Krauter

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.")


Do you have __all__ defined in your __init__.py in
regression.changepassword.unittests?

This part of the tutorial might help:
http://www.python.org/doc/current/tut/node8.html#SECTION008410000000000000000

Rich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top