How can I make this import:
from module import *
with imp module?
Disclaimer:
- I need use
impmodule because I need make several statements and I want to do it dynamicly - I need use
*because I need that in the file I made the import, the variables and methods defined inmodulebe available directly, i meanwithout module.methodormodule.variable. And I want import all variables and methods in the module because I don't know what methods or variables can be in the module in the future
-
2So once you import everything from this mystery module, that you don't know what it contains, what do you plan to do with it? ;-)Bartek– Bartek2011年12月01日 03:18:50 +00:00Commented Dec 1, 2011 at 3:18
-
I'm trying to split django settings.py into several filesdiegueus9– diegueus92011年12月01日 03:23:06 +00:00Commented Dec 1, 2011 at 3:23
-
2Can I recommend this? justcramer.com/2011/01/13/settings-in-djangoBartek– Bartek2011年12月01日 03:27:32 +00:00Commented Dec 1, 2011 at 3:27
-
Of course you can, but I'm triyng something a little bit differentdiegueus9– diegueus92011年12月01日 03:33:17 +00:00Commented Dec 1, 2011 at 3:33
3 Answers 3
Here!
def load_everything_from(module_names):
g = globals()
for module_name in module_names:
m = __import__(module_name)
names = getattr(m, '__all__', None)
if names is None:
names = [name for name in dir(m) if not name.startswith('_')]
for name in names:
g[name] = getattr(m, name)
I am kind of making things up there a little bit with trying to find an __all__ symbol first and then, if that files, doing a dir() and grabbing symbols that look non-private — you would have to look at the implementation of import * to know if that resembles Python's actual logic closely enough for your purposes.
1 Comment
from module_name import * for several module names at once, skipping all the elements with names starting with "_"? Also dir()'s main purpose is interactive prompt and they are mentioning in the documentation that its behaviour may vary across releases. But indeed interesting ;)If you are using django (as mentionned in comments), something like this should work
from django.utils import importlib
my_module = importlib.import_module('app.my_module')
from my_module import *
But I agree that it may be dangerous
1 Comment
from syntax will not work with your local my_module. You will get "my_module is not defined" instead. the import_module is analogous to "from foo import bar" so wildcard imports unfortunately won't workYou can do it by:
from imp import *
but remember:
Explicit is better than implicit.
(from The Zen of Python - read it by using the following command: import this)
6 Comments
import imp in various locations (every import imp statement will be ignored after you first import imp module), you can even re-import module using reload(imp). " because I need made that import several times " is not the reason for importing everything, it is not even relevant, I believe.from imp import * doesn't work for meimport_my_module(name_of_the_module) instead of ability to call import imp anywhere you like in the code?from module import * and I have a list of modules with strings