How would I do the from ... import * with the __import__ function?
The reason being that i only know the name of the file at runtime and it only has 1 class inside that file.
views:
50answers:
1
+5
A:
Don't. Just don't. Do I need to explain just how horrible that it? Dynamically importing (though sometimes inevitable) and importing into global namespace (always avoidable, but sometimes the easier solution and fine withtrusted modules) are bad enough themselves. Dynamically importing into global namespace is... a nightmare (and avoidable).
Just do _temp = __import__(name, globals(), locals(), [name_of_class]); your_class = _temp.name_of_class. According to the docs, this is about what from name import name_of_class would do.
delnan
2010-07-22 19:58:17
ya i got that too after looking at the docs. It didn't work at first, the reason for that is i forgot to declare the your_class as global. Thanks anyway
ultimatebuster
2010-07-22 20:01:45
so in short, you say to use`my_class = __import__(name, globals(), locals(), [name_of_class]).name_of_class`
Nas Banov
2010-07-22 21:14:41