On Thu, Feb 4, 2016 at 4:03 PM, 阎兆珣 <yanzhao...@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 -- https://mail.python.org/mailman/listinfo/python-list