Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > importing modules dynamicly

Reply
Thread Tools

importing modules dynamicly

 
 
dubux
Guest
Posts: n/a
 
      01-11-2011
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!!

 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-11-2011
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
 
Reply With Quote
 
 
 
 
Jean-Michel Pichavant
Guest
Posts: n/a
 
      01-11-2011
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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Importing v reloading modules modules Peter Peyman Puk Python 0 03-19-2010 05:09 PM
How to keep a module with the same name as a module it is importing from importing itself? plb Python 2 02-08-2005 03:14 PM
Importing modules from within other modules Tobiah Python 2 09-14-2003 09:18 PM
Importing/reloading modules OKB (not okblacke) Python 0 09-04-2003 05:25 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57