A solution for this problem is available for Python 2, but it uses the imp
module which is deprecated in Python 3.
imp
has been replaced by importlib
which works well for file based imports. Specifically, importlib.import_module
requires a file name - not a string or a file handler.
I made a workaround by dumping the contents of the URL to a file and importing it
def initlog():
modulename = '_mylogging'
try:
import _mylogging
except ImportError:
r = requests.get('http://(...)/mylogging.py')
with open(modulename+'.py', "w") as f:
f.write(r.text)
finally:
import _mylogging
return _mylogging.MYLogging().getlogger()
but I would like to avoid the intermediate file.
Putting the security, network performance and availability issues aside - is there a way to feed a string to importlib
? (or from a file handler, in which case I would use io.StringIO
)