Importing a package and looping through modules in the package

Discussion in 'Python' started by Dave, Feb 10, 2004.

  1. Dave

    Dave Guest

    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
     
    Dave, Feb 10, 2004
    #1
    1. Advertising

  2. Dave

    Rich Krauter Guest

    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.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
     
    Rich Krauter, Feb 10, 2004
    #2
    1. Advertising

  3. Dave

    Dave Guest

    Rich Krauter <> wrote in message news:<>...
    > 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.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


    Hi Rich,

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

    Thanks,
    Bardia
     
    Dave, Feb 10, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tobiah
    Replies:
    2
    Views:
    314
    Tobiah
    Sep 14, 2003
  2. Brian Quinlan
    Replies:
    2
    Views:
    288
    Roger Upole
    Apr 27, 2006
  3. Tim Golden
    Replies:
    1
    Views:
    325
    Roger Upole
    Apr 27, 2006
  4. Peter Peyman Puk

    Importing v reloading modules modules

    Peter Peyman Puk, Mar 19, 2010, in forum: Python
    Replies:
    0
    Views:
    302
    Peter Peyman Puk
    Mar 19, 2010
  5. Aaron
    Replies:
    2
    Views:
    533
    dhtml
    Apr 10, 2011
Loading...

Share This Page