[Tutor] Running files from command prompt

Alexander Quest redacted@example.com
Fri Jul 29 05:28:13 CEST 2011


To clarify, the particular file that was giving me trouble was the basic
"hello world" file. The original code on line 29 read as such: print
'Hello', name
When I ran "C:\google-python-exercises> python hello.py, it gave me an error
on that line (line 29), but when I changed that line to print ('Hello',
name), that is, including the parentheses, it printed out "hello world" as
it should. I'm assuming that this means that one of the differences between
Python 2.X and Python 3.X is that the print function necessitates
parentheses in the latter versions but not in the former. I am a bit
confused as to why this is, assuming I am correct in my assumption above,
because I was under the impression that code written for earlier python
versions will work for later python versions, as is the case here. Anyways,
I just wanted to add this info to clarify my last question regarding whether
or not I should install Python 2.X and uninstall Python 3.1 that I have now,
since I'm guessing that doing the google exercises will continue to give me
these errors with other programs (but this is, of course, still assuming
that the error cited above truly is caused by version incompatibility).

-Alex


On Thu, Jul 28, 2011 at 7:58 PM, Alexander Quest <redacted@example.com>wrote:

> Awesome- thanks for that Dave! The programs all work now, except that the
> google exercise programs are all from Python 2.X and I'm running 3.1, so
> some of them are giving me errors. Is there a way around this or do I have
> to download a 2.X version so I can run these without a problem? Thanks
> again.
>
> -Alex
>
>
> On Thu, Jul 28, 2011 at 7:11 PM, Dave Angel <d at davea.name> wrote:
>
>> 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<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
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110728/66febe95/attachment.html>


More information about the Tutor mailing list
html>