Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

eval( 'import math' )

63 views
Skip to first unread message

阎兆珣

unread,
Feb 4, 2016, 8:58:42 AM2/4/16
to
Excuse me for the same problem in Python 3.4.2-32bit

I just discovered that <eval()> function does not necessarily take the
string input and transfer it to a command to execute.

So is there a problem with my assumption?

Thanks

Option Product Manager

Gelin Dahua Futures Co.Ltd

 T: +86 10 56711783

Lutz Horn

unread,
Feb 4, 2016, 9:10:30 AM2/4/16
to
Hi,

> I just discovered that <eval()> function does not necessarily take the
> string input and transfer it to a command to execute.

Can you please show us the code you try to execute and tells what result you expect?

Lutz

Ian Kelly

unread,
Feb 4, 2016, 9:31:16 AM2/4/16
to
On Thu, Feb 4, 2016 at 6:33 AM, 阎兆珣 <yanzh...@greendh.com> wrote:
> Excuse me for the same problem in Python 3.4.2-32bit
>
> I just discovered that <eval()> function does not necessarily take the
> string input and transfer it to a command to execute.
>
> So is there a problem with my assumption?

eval evaluates an expression, not a statement. For that, you would use exec.

If you're just trying to import though, then you don't need it at all.
Use the importlib.import_module function instead to import a module
determined at runtime. This is more secure than eval or exec, which
can cause any arbitrary Python code to be executed.

Peter Otten

unread,
Feb 4, 2016, 9:49:05 AM2/4/16
to
阎兆珣 wrote:

> Excuse me for the same problem in Python 3.4.2-32bit
>
> I just discovered that <eval()> function does not necessarily take the
> string input and transfer it to a command to execute.
>
> So is there a problem with my assumption?

Python discriminates between statements and expressions. The eval function
will only accept an expression. OK:

>>> eval("1 + 1")
2
>>> eval("print(42)") # Python 3 only; in Python 2 print is a statement
42
>>> x = y = 1
>>> eval("x > y")
False

Not acceptable:

>>> eval("1+1; 2+2")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
1+1; 2+2
^
SyntaxError: invalid syntax

>>> eval("import os")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
import os
^
SyntaxError: invalid syntax

>>> eval("if x > y: print(42)")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
if x > y: print(42)
^
SyntaxError: invalid syntax

To import a module dynamically either switch to exec()

>>> exec("import os")
>>> os
<module 'os' from '/usr/lib/python3.4/os.py'>

or use the import_module() function:

>>> import importlib
>>> eval("importlib.import_module('os')")
<module 'os' from '/usr/lib/python3.4/os.py'>

Of course you can use that function directly

>>> importlib.import_module("os")
<module 'os' from '/usr/lib/python3.4/os.py'>

and that's what you should do if your goal is to import a module rather than
to run arbitrary Python code.

0 new messages