Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Weird newbie question

16 views
Skip to first unread message

Matty Sarro

unread,
Jan 26, 2012, 5:05:57 PM1/26/12
to Python list
Hey everyone. I'm running into a funky error as I work through "Learn
Python the Hard Way." What's weird is both idle and the python
interpreter in idle spit out an error about syntax, but when I run the
same script from the command line it works just fine, with no issue.
I'm not sure if its an issue with IDLE or if I'm doing something
wrong.

Here's the book's code:

from sys import argv
script, filename = argv
txt = open(filename)
print "Here's your file %r:" % filename
print txt.read()
print "Type the filename again:"
file_again = raw_input("> ")
txt_again = open(file_again)
print txt_again.read()

Here's my code:

from sys import argv
script,filename=argv
txt=open(filename)
print "Here is your file %r:" % filename
print txt.read()
print "I'll also ask you to type it again:"
file_again=raw_input("> ")
txt_again=open(file_again)
print txt_again.read()


IDLE is saying that my error is on line 4, at the second set of
quotation marks. Since I can't get the error from the command line, I
can't actually see what the syntax error is that it's complaining
about. Any advice would really be appreciated, thank you!!

Andrea Crotti

unread,
Jan 26, 2012, 5:11:11 PM1/26/12
to Matty Sarro, pytho...@python.org
Let me try, maybe you're running the IDLE for Python 3 and the shell for
python 2?

Steven D'Aprano

unread,
Jan 26, 2012, 5:27:11 PM1/26/12
to
On Thu, 26 Jan 2012 17:05:57 -0500, Matty Sarro wrote:

> Hey everyone. I'm running into a funky error as I work through "Learn
> Python the Hard Way." What's weird is both idle and the python
> interpreter in idle spit out an error about syntax, but when I run the
> same script from the command line it works just fine, with no issue.

Either you are mistaken about it being the same script, or you have two
versions of Python installed, Python 2.x and Python 3.x.

My guess is that you have two versions installed, and when you run IDLE
you are running Python 3, and when you run the script from the command
line you are running Python 2.


> I'm
> not sure if its an issue with IDLE or if I'm doing something wrong.
>
> Here's the book's code:

Irrelevant. Unless you think that somehow your computer is running the
code in the book instead of the code in your computer?


> Here's my code:
>
> from sys import argv
> script,filename=argv
> txt=open(filename)
> print "Here is your file %r:" % filename
> print txt.read()
> print "I'll also ask you to type it again:"
> file_again=raw_input("> ")
> txt_again=open(file_again)
> print txt_again.read()
>
>
> IDLE is saying that my error is on line 4, at the second set of
> quotation marks.

Line four is not the second set of quotation marks. It is the first set
of quotation marks.

Please show the ENTIRE error displayed, including the full traceback, and
not just the message. That is, everything starting from "Traceback (most
recent call last)" to the end. Please copy and paste the full error, do
not summarise or paraphrase it.


> Since I can't get the error from the command line,

Earlier you said that the script works correctly when you run it from the
command line.


> I
> can't actually see what the syntax error is that it's complaining about.
> Any advice would really be appreciated, thank you!!

(1) Inside IDLE, type this:

import sys
sys.version

What does it show?

(2) Show us the exact command you give on the command line that
successfully runs the script.


There may be more questions later, but this will do to start.



--
Steven

Ethan Furman

unread,
Jan 26, 2012, 5:19:43 PM1/26/12
to Matty Sarro, Python list
Matty Sarro wrote:
> from sys import argv
> script,filename=argv
> txt=open(filename)
> print "Here is your file %r:" % filename
> print txt.read()
> print "I'll also ask you to type it again:"
> file_again=raw_input("> ")
> txt_again=open(file_again)
> print txt_again.read()
>
> IDLE is saying that my error is on line 4, at the second set of
> quotation marks.

The code is for Python 2 -- are you using Python 3?

~Ethan~

John Gordon

unread,
Jan 26, 2012, 5:35:15 PM1/26/12
to
This may be a Python version mismatch error.

In python version 2.x (and 1.x for that matter), "print" is a statement,
but it was changed to be a function in python version 3.x.

In other words, in python 2.x you can say this:

print x
print "hello there"

But in python 3.x you have to do it a little differently:

print(x)
print("hello there")

It sounds like your IDLE is running python 3, but your command line is
running python 2.

--
John Gordon A is for Amy, who fell down the stairs
gor...@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Rick Johnson

unread,
Jan 26, 2012, 5:53:54 PM1/26/12
to
On Jan 26, 4:05 pm, Matty Sarro <msa...@gmail.com> wrote:

> Here's my code:
>
> from sys import argv
> script,filename=argv
> txt=open(filename)
> print "Here is your file %r:" % filename
> print txt.read()
> print "I'll also ask you to type it again:"
> file_again=raw_input("> ")
> txt_again=open(file_again)
> print txt_again.read()
>
> IDLE is saying that my error is on line 4, at the second set of
> quotation marks. [...]

Forget about line 4 because line 2 is a problem waiting to happen
(PEP8 formating added for clarity)

Line2: script, filename = argv

Is this a case of the blind leading the blind?

Matty Sarro

unread,
Jan 26, 2012, 8:51:43 PM1/26/12
to Rick Johnson, pytho...@python.org
The issue was a version mismatch. When installing python on windows,
it installs a shortcut so when you right click, you can edit things in
IDLE. I had installed python 3 after installing 2.7, so it apparently
changed the extension.

As for not showing tracebacks, I couldn't. When I ran the program from
the command line, it ran the version 2.7 python because that's the one
I have set in my $PATH. However when I was editing it in IDLE, it
would highlight the second set of quotation marks in the statement and
simply spit out an error saying "Invalid Syntax." There was no
traceback because IDLE wouldn't run the program in the interpreter
that opens along side the editor.

Re-opening the file in the 2.7 version of IDLE removed any issues.

Thanks for your help everyone.
> --
> http://mail.python.org/mailman/listinfo/python-list
0 new messages