Puzzled

Peter Otten __peter__ at web.de
Sun Nov 1 05:13:51 EST 2015


Robinson, Wendy wrote:

> Hi there,
> I installed Python 3.5.0 64-bit for Windows yesterday and tried some basic
> programs successfully.
> This morning I rebooted my computer and can't get a single one to work. 
> The interpreter seems to be fine and the environment variables look
> correct.  But every py file I try to run at the >>> prompt gives me a
> NameError.
> 
> I tried running the Repair installation, but that did not help.
> 
> Any suggestions?

If you want to run a python script you have to invoke it on the commandline, 
not inside the interactive interpreter, i. e. if you have a script 
myscript.py containing the line

print("Hello world!")

Wrong:

C:\> python3
>>> myscript.py
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'myscript' is not defined

Here python looks for a variable "myscript" which of course isn't defined. 
(If it were the next step would be to look for an attribute "py" and you'd 
probably get an AttributeError)

Right:

C:\> python3 myscript.py
Hello world!

Here python runs the script and exits.

If you have a function defined in a module that you want to use from the 
interactive interpreter you have to import the module. Assuming mymodule.py
contains the function

def say_hello():
    print("Hello world!")

C:\> python3
>>> import mymodule
>>> mymodule.say_hello()
Hello world!




More information about the Python-list mailing list