Weird newbie question

M

Matty Sarro

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!!
 
S

Steven D'Aprano

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.
 
J

John Gordon

In said:
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!!

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.
 
R

Rick Johnson

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?
 
M

Matty Sarro

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.

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top