How does one do the equivalent of “import * from module” with Python's __import__ function?

前端 未结 5 648
温柔的废话
温柔的废话 2020-11-29 03:51

Given a string with a module name, how do you import everything in the module as if you had called:

from module import *

i.e. given string

5条回答
  •  有刺的猬
    2020-11-29 04:30

    I didn't find a good way to do it so I took a simpler but ugly way from http://www.djangosnippets.org/snippets/600/

    try:
        import socket
        hostname = socket.gethostname().replace('.','_')
        exec "from host_settings.%s import *" % hostname
    except ImportError, e:
        raise e
    

提交回复
热议问题