Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import a module in Python 3 from a string?

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)

like image 896
WoJ Avatar asked Oct 20 '25 18:10

WoJ


1 Answers

You can adapt exactly the same answer to 3.x, using the replacement for imp.new_module:

from types import ModuleType

foo = ModuleType('foo')

and the replacement for the exec statement:

foo_code = """
class Foo:
    pass
"""

exec(foo_code, globals(), foo.__dict__)

After which everything works as expected:

>>> dir(foo)
['Foo', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
>>> foo.Foo()
<__main__.Foo object at 0x110546ba8>
like image 115
jonrsharpe Avatar answered Oct 23 '25 07:10

jonrsharpe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!