I'm trying to do an __init__.py that load module and instantiate dinamically internal namesake class. With a file tree like
importer/
__init__.py # The file i'm writing
asd.py # It contains class asd
bsd.py # It contains class bsd
And __init__.py with
importername=__name__
def load(modname,paramlist = []):
if modname in globals():
print 'Module %s already loaded.' % (modname)
else:
imported_mod = __import__(importername + '.' + modname, fromlist = ["*"])
try:
globals()[modname]=getattr(imported_mod,modname)(*paramlist)
except Exception, e:
print 'Module %s not loaded: %s.' % (modname, e)
If I run
import importer
importer.load('asd')
print importer.asd.name
'ASD!'
It works like a charm, but if I run
from importer import *
load('asd')
print asd.name
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'asd' is not defined
Can I fix it in some way? Thank you.