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

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')

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())

Related

Python script using PIL works in IDE doesn't when run by cmd and gives 'PIL' has no attribute 'Image'

When I run the following in the Spyder IDE on Win10:
import PIL
import os
Inboundfile = "C:/Test/test/Overlay.png"
image = PIL.Image.open(os.path.abspath(Inboundfile))
image.show()
It works fine. Opens the picture and outputs:
runfile('C:/Users/bob/Anaconda3/envs/NewEnvironment/bob/test3.py', wdir='C:/Users/bob/Anaconda3/envs/NewEnvironment/bob')
But when I call the same script from a cmd prompt:
C:\Users\bob\Anaconda3\envs\NewEnvironment\bob>python test3.py
It results in:
Traceback (most recent call last):
File "test3.py", line 14, in <module>
image = PIL.Image.open(os.path.abspath(Inboundfile))
AttributeError: module 'PIL' has no attribute 'Image'
I have verified that the environment path is pointing to the same python environment as the IDE. ( I think ) Here is my Path variable from OS
C:\Users\bob\Anaconda3\envs\NewEnvironment\
It's probably something very simple but I'm very new at this and any help would be appreciated.
I tried adding the line:
from PIL import Image
It still runs fine in the IDE but the error message changes to:
C:\Users\bb\Anaconda3\envs\NewEnvironment\bob>python test3.py
Traceback (most recent call last):
File "test3.py", line 11, in <module>
from PIL import Image
File "C:\Users\bob\Anaconda3\envs\NewEnvironment\lib\site-packages\PIL\Image.py", line 69, in <module>
from . import _imaging as core
ImportError: DLL load failed: The specified module could not be found.
This is even more confusing as I can follow that path and see Image.py sitting in the directory. What is the IDE doing that CMD is not?
All of my problems vanished when I simply closed and restarted the console I was using to call the python script. I have learned that the console saves things like the path variables when it starts up and must be restarted to get changes.

Error on calling a function from another file on python

I'm getting an error on importing a function from another file in python.
This is my file structure
In checkSpellings.py there's a function defined as spellchecker(tokenized_text) . And I'm trying to use it by importing it on main.py by following code
from checkSpellings import spellChecker
But it gives me an warning (red underline on top of both of checkSpellings(file name) and spellChecker(function name) in import code). This previously happened and I though this is purely an issue with intellisense . Because last time same thing happened (gave me the warning), but the code worked fine.
But now when I run the main.py it gives me an error saying
Traceback (most recent call last):
File "/home/pankaja/Documents/PycharmProjects/BookDigitizer/OCR_Correction/main.py", line 1, in <module>
from checkSpellings import spellChecker
ImportError: cannot import name 'spellChecker
How can I fix this ? What have I done wrong ?
IMPORTANT : Here I'm using python3.6 interpreter on an anaconda virtual environment. Might it be an issue ?
The function is spellchecker but you tried to import spellChecker.

ImportError with different python installations.

I am getting below ImportError while running a python file.
ImportError: No module named osgeo.gdal_array
!/usr/bin/env python
from osgeo.gdal_array import BandReadAsArray
However, if i try to import same from command line, it runs fine.
$ which python
/home/hduser/anaconda2/bin/python
$ python
>>> from osgeo.gdal_array import BandReadAsArray
>>>
Also, please see the below where i am getting the same ImportError.
$ /usr/local/bin/python2.7
>>> from osgeo.gdal_array import BandReadAsArray
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named osgeo.gdal_array
I figured out that there is something going on between different versions of python. But, i do not want to change the original source code.
How do i make my program run without changing anything within the code of calling python installed in anaconda explicitly?

Run a Python script from Python prompt such that variables are loaded into the interactive environment

Say I have a (somewhat pointless) Python script
#!/usr/bin/python
a = 5
Is there a way to run this script from the interactive prompt such that after running if I type a I get
>>> a
5
and not
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
More generally, if my script calculates something through a series of steps, is there a way to access that something after the script has finished (in the same kind of manner).
Import it:
from yourscriptname import a
Each and every .py file in python is a module, and you can simply import it. If the file is called foo.py, import foo.

Unable to debug interactively with Python 2.7

I had asked a question previously on relative paths in python in SO question: How can I access relative paths in Python 2.7 when imported by different modules
The provided answer worked great in all of my scripts and functions. However, when trying to debug the files in IDLE (Python 2.7) it generates run time errors.
Can anyone point me to documentation on using the __file__ notation? Also I would like to understand why IDLE generates errors while running the sample code but running the same file from the command line or double clicking it (for the windows users) does not.
Any help would be greatly appreciated!
Note that I am running Python 2.7 on Windows XP with virtualenv (unactivated during these tests).
Sample Code
import os
import sys
curdir = os.path.dirname(__file__)
sys.path.append(curdir + '/..')
Error
Traceback (most recent call last):
File "C:\MyFile.py", line 3, in `<module>`
curdir = os.path.dirname(`__file__`)
NameError: name '`__file__`' is not defined
__file__ won't be set if you're writing this in the interpretor.
So:
>>> import os, sys
>>> curdir = os.path.dirname(__file__)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '__file__' is not defined
Is expected.
__file__ is the name of the file that was called by the python interpretor - so if you ran this from a script it would work.
$ python curdir.py
$
(The script is exactly the same as what I put into the interpretor, hence no error or output)
From what I've observed using IDLE before, it acts as an interpretor - so it'll run the file in question. However, it wasn't started with that file, so the __file__ is never set.

Categories