Steven D'Aprano added the comment:

That is not a bug, it is a feature. `eval` only evaluates *expressions*, not 
statements, and `import` is a statement. Neither of those is going to change.

Starting in Python 3.1, you could use `import_module`:

https://docs.python.org/3/library/importlib.html#importlib.import_module

There's no really great solution for Python 2, you could use the built-in 
`__import__` function, but being a dunder function it is for Python's internal 
use, not really intended for normal use. Or write a small helper function:

    # untested, and beware of security implications!
    # don't use this on untrusted input
    def import_module(name):
        ns = {}
        exec("import " + name, ns, ns)
        return ns[name]

Now you can use that function in `eval`.

----------
nosy: +steven.daprano
resolution:  -> not a bug
status: open -> closed

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue27757>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to