1

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 e.g.:

runfile('E:/Anyagok/Programozas/Python/projekts/gyak/Pyautogui/simaref.py', wdir='E:/Anyagok/Programozas/Python/projekts/gyak/Pyautogui')
Chris
  • 951
  • 10
  • 26
  • 1
    Spyder has a global namespace shared between your console and the script itself. It's likely that you imported runfile at some point in the past, somewhere, and that name is now permanently in the namespace so the name can always be resolved, even for code that wouldn't otherwise work. You should try explicitly importing it inside `simaref.py` and see if that fixes the issue outside of Spyder. – roganjosh Apr 02 '18 at 17:05
  • 1
    What's `runfile` and where does it come from? Why would you _expect_ it to be defined? – Aran-Fey Apr 02 '18 at 17:10
  • My guess is that this is related to the issue [here](https://stackoverflow.com/q/47738537/4799172) but can't be sure. Point 2 in my answer will make Spyder behave more rationally so that these issues don't slip under the radar until you take scripts out of the environment. Does it work? – roganjosh Apr 02 '18 at 17:19
  • @Aran-Fey When I run a script in Spyder, it shows in the Console the runfile() command, so I thought why not use it – Chris Apr 02 '18 at 17:20

1 Answers1

0

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())
Chris
  • 951
  • 10
  • 26