import cannot be used inside eval
Chris Angelico
rosuav at gmail.com
Thu Feb 4 03:48:43 EST 2016
On Thu, Feb 4, 2016 at 4:03 PM, 阎兆珣 <yanzhaoxun at greendh.com> wrote:
> a = input("tell me which py to execute: ")
>
> print(a)
>
> print('import '+a)
>
> print(type('import'+a))
>
> eval('print(a)')
>
> try:
>
> eval('import '+a)
>
> except Exception as e:
>
> print('Error: ', e)
>
> ##while True:
>
> ## pass
>
> @python 3.4.2
>
> this code attempts to run a .py file that user specifies.
>
> eval() does fine with print() command
>
> but fails to call import command
The eval() function evaluates an expression. It won't handle
statements (if, for, while), and import is a statement. Possibly you
want exec instead.
ChrisA
More information about the Python-list
mailing list