简体   繁体   中英

NameError: name 'runfile' is not defined only when running from script

sima.py

import pyautogui as py
py.alert("Foo")

simaref.py

runfile('E:/Anyagok/Programozas/Python/projekts/gyak/Pyautogui/sima.py', 
        wdir='E:/Anyagok/Programozas/Python/projekts/gyak/Pyautogui')

These both work when running from Spyder.

sima.py works from cmd as well:

python.exe E:\Anyagok\Programozas\Python\projekts\gyak\Pyautogui\sima.py

But simaref.py doesn't:

E:\Download\PROGIK\ANACONDA>python.exe E:\Anyagok\Programozas\Python\projekts\gyak\Pyautogui\simaref.py
Traceback (most recent call last):
  File "E:\Anyagok\Programozas\Python\projekts\gyak\Pyautogui\simaref.py", line 8, in <module>
    runfile('E:/Anyagok/Programozas/Python/projekts/gyak/Pyautogui/sima.py', wdir='E:/Anyagok/Programozas/Python/projekts/gyak/Pyautogui')
NameError: name 'runfile' is not defined

Why not?

Edit: Got the idea of runfile() from: when running code in Spyder, in the console it displays eg:

runfile('E:/Anyagok/Programozas/Python/projekts/gyak/Pyautogui/simaref.py', wdir='E:/Anyagok/Programozas/Python/projekts/gyak/Pyautogui')

Working with roganjosh's answer: Couldn't find where to import the runfile() command, maybe it's something built-in. So I changed simaref.py to the code below, and now it works.

exec(open("E:/Anyagok/Programozas/Python/projekts/gyak/Pyautogui/sima.py").read())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM