1

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.

2
  • Dynamically updating global variables is considered bad practice in Python. What do you actually want this for? There's probably a better way. Commented May 10, 2011 at 21:37
  • Look at my comment in Ned answer. Commented May 10, 2011 at 22:08

1 Answer 1

2

There is no such thing as truly global in Python. When you use globals() in importer/__init__.py, you are accessing the module's namespace. Your second non-working sample is adding 'asd' to importer, not to your test module.

The best you could is modify importer so you could do:

from importer import *
asd = load('asd')
print asd.name

But as Thomas K suggests: this is kind of odd, perhaps there's a simpler way to solve your problem?

Sign up to request clarification or add additional context in comments.

7 Comments

I need a sort of set of singleton classes, dynamically loadable, and reachable in every part of the program, like as import statement offer.
@Emilio: Um, that doesn't make it clearer, at least to me. What problem are you actually trying to solve?
I need a sort of plugin factory, loaded a single time, that have to load and instantiate dinamically plugins in a directory (with .load() and .unload()). The 'import' way is ok because i need to reach this factory from everywhere in the code.
@Emilio: Can you, for example, store the loaded plugins in a list or a dict? You could even subclass dict, so that when you do asd = plugins['asd'], it loads the asd plugin if it isn't already loaded. Loading plugins is the easy bit: working out the best way to reference them in your code is what's interesting.
If i initialize this plugin{} dict in a module moddict.py, i can import it from everywhere calling import moddict and using as moddict.plugin['pluginname'].method(). And it's ok, but is it cleaner than my solution?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.