importing modules dynamicly

Discussion in 'Python' started by dubux, Jan 11, 2011.

  1. dubux

    dubux Guest

    I am trying to import modules dynamicly from a directory (modules/) in
    which i have __init__.py with the __all__ variable set. Everything
    imports correctly and I have verified this however I am stuck on
    actually using my classes in the dynamicly imported modules.

    this bit is in my main.py (or base script) to import the modules in
    the modules/ directory:

    loaded_modules = []
    for item in modules:
    if item == '__init__.py': pass
    else:
    if item.endswith('.py'):
    __import__('modules.' + item[0:len(item) - 3])
    loaded_modules.append(item[0:len(item) - 3])
    else: pass

    After loading all the modules, i try to do something like:

    instance = modules.modulename.class()

    And I get an AttributeError. What am I doing wrong here? Help please!!
    dubux, Jan 11, 2011
    #1
    1. Advertising

  2. On Mon, 10 Jan 2011 20:42:17 -0800, dubux wrote:

    > After loading all the modules, i try to do something like:
    >
    > instance = modules.modulename.class()


    No you don't. class is a reserved word in Python, you would get a
    SyntaxError if you did that.

    Please post the error you get, including the complete traceback, showing
    the line of code that fails. Copy and paste the *actual* message in full,
    don't retype it from memory, paraphrase it, simplify it, translate it
    into Swahili, or otherwise change it in anyway.



    --
    Steven
    Steven D'Aprano, Jan 11, 2011
    #2
    1. Advertising

  3. dubux wrote:
    > I am trying to import modules dynamicly from a directory (modules/) in
    > which i have __init__.py with the __all__ variable set. Everything
    > imports correctly and I have verified this however I am stuck on
    > actually using my classes in the dynamicly imported modules.
    >
    > this bit is in my main.py (or base script) to import the modules in
    > the modules/ directory:
    >
    > loaded_modules = []
    > for item in modules:
    > if item == '__init__.py': pass
    > else:
    > if item.endswith('.py'):
    > __import__('modules.' + item[0:len(item) - 3])
    > loaded_modules.append(item[0:len(item) - 3])
    > else: pass
    >
    > After loading all the modules, i try to do something like:
    >
    > instance = modules.modulename.class()
    >
    > And I get an AttributeError. What am I doing wrong here? Help please!!
    >
    >

    Your code is rather strange, 'modules' looks to be a list or some
    iterable, and then you expect to have a 'modulename' attribute or
    something...
    My guess is that you pasted an approximative translation of your code
    which makes it impossible de debug.

    Here is a possible way of importing a bunch of python files:

    loaded_modules = {}
    fileNames = os.listdir('./modules')
    pyFiles = [os.path.basename(name).replace('.py', '') for name in
    fileNames if name.endswith('.py')]

    for pyFile in pyFiles:
    loaded_modules[pyFile] = __import__('modules.%s' % pyFile)

    # how to get a class named 'AClassName' defined in then modules
    for module in loaded_modules:
    myClass = getattr(loaded_modules[module], 'AClassName', None)
    print myClass
    if myClass:
    myInstance = myClass()

    JM
    Jean-Michel Pichavant, Jan 11, 2011
    #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. OKB (not okblacke)

    Importing/reloading modules

    OKB (not okblacke), Sep 4, 2003, in forum: Python
    Replies:
    0
    Views:
    284
    OKB (not okblacke)
    Sep 4, 2003
  2. Tobiah
    Replies:
    2
    Views:
    297
    Tobiah
    Sep 14, 2003
  3. Dave
    Replies:
    2
    Views:
    447
  4. plb
    Replies:
    2
    Views:
    317
  5. Peter Peyman Puk

    Importing v reloading modules modules

    Peter Peyman Puk, Mar 19, 2010, in forum: Python
    Replies:
    0
    Views:
    285
    Peter Peyman Puk
    Mar 19, 2010
Loading...

Share This Page