[Tutor] Running files from command prompt

Dave Angel d at davea.name
Fri Jul 29 04:11:39 CEST 2011


On 07/28/2011 09:58 PM, Alexander Quest wrote:
> I downloaded the google's python exercise files from their website (
> http://code.google.com/edu/languages/google-python-class/set-up.html),
> unzipped them, and placed them in C.
> I then added the following to the PATH variable under system settings so
> that I could type "python" in command prompt and have Windows start the
> interpreter: C:\Python31;C:\Python31\Tools\Scripts
>
> When I type in "python" in the command prompt, the interpreter opens, but
> when I try to open one of the programs from the Google exercise files
> (hello.py), I get the following error:
> Traceback<most recent call last>:
>     File "<stdin>", line 1, in<module>
> NameError: name 'hello' is not defined
>

When you're running the python interpreter, you can't just type the name 
of your script.  You need to import it
      import hello

However, first it needs to be in the python's module search path.  
Easiest way is to make
  it your current directory.

So, from a command prompt:

cd C:\google-python-exercises

python
XXXX starting Python version ....

 >>>>import hello


> Or, if I just type in "python hello.py" first in the command prompt (as
> opposed to typing in python, hitting enter, and THEN typing in hello.py, as
> above), I get the following error:
>
>
> python: can't open file 'hello.py': [Errno 2] No such file or directory.
>
> So I guess my question is how do I run .py files from the command prompt now
> that I seem to have gotten Windows to recognize and open the interpreter
> when I type in "python"? Thanks for any help.
>
Similarly, before running python, change to the directory you want the 
script to run in.
Normally, you'd do:

cd c:\google-python-exercises
python hello.py


> -Alex
>
> P.S. Just as an aside, when I open up the command prompt, the initial
> directory is C:\Users\Alexander, but my google exercises are in
> C:\google-python-exercises and python itself is in C:\Python31. I don't know
> if this makes a difference or not.
>


-- 

DaveA



More information about the Tutor mailing list