By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,786 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,786 IT Pros & Developers. It's quick & easy.

Importing a package and looping through modules in the package

P: n/a
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.unit tests.")
----
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
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Mon, 2004-02-09 at 22:29, Dave wrote:
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.unit tests.")

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/tu...00000000000000

Rich
Jul 18 '05 #2

P: n/a
Rich Krauter <rm*******@yahoo.com> wrote in message news:<ma***************************************@py thon.org>...
On Mon, 2004-02-09 at 22:29, Dave wrote:
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.unit tests.")

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/tu...00000000000000

Rich


Hi Rich,

Thanks for your response. That was exactly what the problem was. I
should have gone through the tutorial more carefully :).

Thanks,
Bardia
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.