[Tutor] Python Shell

Alan Gauld alan.gauld at yahoo.co.uk
Sun Oct 9 04:08:42 EDT 2016


On 08/10/16 20:43, Alan Clarke wrote:

> I created a program called HW.py  that runs under a command prompt, 
> but Python Shell gives the following error:

How are you running it in the Python shell?

> Traceback (most recent call last):
>   File "<pyshell#7>", line 1, in <module>
>     HW.py
> NameError: name 'HW' is not defined
> 
> Can someone please tell me what I'm doing wrong?

Since you don't tell us what you are doing, we
cant tell you what about it is wrong.

If you are are trying to execute HW.py from
the >>> prompt by just typing its name
then that's wrong. You need to import it as HW

import HW   # note not HW.py, just HW

And if it has a sentinel section that runs
a main() function then you need to replicate
that code manually.

But it's not normal to run programs from the
Python shell like that. Most IDEs such as
IDLE have a menu option to run the program
and the output then appears in the shell
window. But, even then, it should just be for
testing, the best way to run the program
is, as you apparently did, using the
python interpreter directly from the OS.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list