I would like to construct a function that emulates from package_name import *
. While this question answers how one might do so by modifying globals()
. globals()
, however, is local to the module the function is defined in.
For example, suppose I define the following function in first_package.py
,
import imp
def load_contents(path):
"""Load everything from another package"""
module = imp.load_source('', path)
for k in dir(module):
if not '__' in k: # ignore __xxx__ private variables
globals()[k] = getattr(module, k)
Then run the following code in second_package.py
import first_package
first_package.load_contents('third_package.py')
Nothing will happen. How can I fix this?