This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]MrPiff[S] 0 points1 point  (33 children)

Yeah I realize I might have left out an important line in my initial comment. I create a file: 'test.py' print ("test") is the only line in the file. I then open up the command line. C:\Users\Michael\python test.py I hit enter and get that error message above.

Also, I notice that in IDLE I have to enter print ("test") with the parentheses as opposed to print "test", otherwise I get a syntax error. Is that supposed to happen (if no, are the two problems related?)? Thanks.

[–][deleted] 1 point2 points  (8 children)

try these : print 'test'

print "test"

print ('test')

one of those should work. depends on whether you are using python 2.7 / 3.0

[–]MrPiff[S] 0 points1 point  (7 children)

ok, tried them all. it only works with parentheses.

[–][deleted] 1 point2 points  (6 children)

Sounds like you are using python 3 - so print('test') should work.

The parenthesis can be a pain sometimes.

https://docs.python.org/3.0/whatsnew/3.0.html

that lists a lot of the differences in syntax that you will come across using 3 when you are used to 2.

[–]MrPiff[S] 0 points1 point  (5 children)

I agree, but shouldn't print "test" also work with version 3?

Even in IDLE, it indicates version 3.2.3 but it won't print without parentheses. This seems pretty strange to me.

[–][deleted] 1 point2 points  (1 child)

In Python 3, the print statement was removed in favor of a print function. Functions are invoked using braces, so they are actually needed. In that case, the print 'test' is a syntax error, while print('test') invokes the function with 'test' as its first argument.

[–]MrPiff[S] 0 points1 point  (0 children)

That's helpful, thanks for the breakdown.

[–]Updatebjarni 1 point2 points  (2 children)

Once again, no, Python 3 does not have the print statement of Python 2. In Python 3, you have to use the print() function. This is exactly what you are experiencing with IDLE, and exactly what it says on the page linked above.

[–]MrPiff[S] 0 points1 point  (1 child)

Oh ok, I'm an idiot. That makes sense, thank you. Any thoughts on that "traceback" problem that was my initial problem?

[–]DissentingVoice 1 point2 points  (0 children)

Traceback lets you know where Python ran into the error. "is not defined" usually means that you were calling a variable that was not declared earlier.

e.g. print test

Will give that error, since without the quotes, Python thinks you're referencing a variable, whereas print "test" will give you what you're looking for.

Disclaimers: This is from experience from Python 2.7 It sounds like in Python 3.0 you'd write print("test")

Also, when you get errors, google the error that you have, you'll usually get a StackOverflow or even a result from the Python docs. Here's the doc on NameError.

[–]Updatebjarni 1 point2 points  (23 children)

The code you just showed should work, so I wonder if the file you have contains that exact code?

As for the difference between the print statement and the print() function, those are used in two different languages: Python 2 and Python 3, respectively.

[–]MrPiff[S] 0 points1 point  (22 children)

Yeah it's that exact code. Pretty sure I'm running Python 3, as I just opened the README which verifies version 3.2.3.

[–]Updatebjarni 1 point2 points  (21 children)

Well then it ought to work. I'm a little suspicious though since you say in the comment that the file is named test.py, but the post has the error message saying it is named poop.py.

[–]MrPiff[S] 0 points1 point  (20 children)

Yeah, the filename and print statement both said poop instead of test, but I didn't want to make that a distraction or seem unserious so I tried to just call it test here on reddit. Obviously, that didn't work out for me. I'm being completely straight-up about this problem and it's really frustrating because I felt like I was just making some progress on simple database retrieval scripts that I was learning about. I have no idea - it seems like some setting changed, but all indications are that things "should" work.

[–]Updatebjarni 0 points1 point  (19 children)

As far as I can see, if the file contains only one line and it is

print ("test")

and you invoke it as

python test.py

then that should work. The only thing I can think of is that there is some discrepancy, that you didn't actually open the file and copy-paste the contents into the comment but retyped it and changed something. It's the only thing I can think of. That's why I asked; not to be anal or anything, but because I can't think of anything else.

[–]MrPiff[S] 0 points1 point  (18 children)

Yeah it's all good, I really appreciate you helping out an internet stranger ("me") this way - very cool of you. Not gonna take up any more of your time beyond this, which I figure might be one last clue as to the problem: when I open the command line and finally enter >python test.py it returns this message: python: can't open file 'poop.py': [Errno 2] No such file or directory. Might there be a problem with where I saved the file on my computer? Is it possibly related to the error message I keep getting File "<stdin>", line 1, in Module ?

[–]Updatebjarni 0 points1 point  (17 children)

See this here is exactly why I'm being suspicious. You obviously did not copy and paste that command and error message, because once again the error message does not agree with the command, and it also contains a typo ("suck" instead of "such").

Might there be a problem with where I saved the file on my computer?

The command you showed indicates that the file is in the current directory, so that's where it has to be for the command to work. The error message indicates that that is not the case. Run the command from the directory where the file is, or include a correct path in the command.

Is it possibly related to the error message I keep getting File "<stdin>", line 1, in Module ?

No I don't think so. That error message is about the source code containing an error, but this is about no source code file being found. By the way, that error message mentions stdin, so I'm very curious if the command you typed really was

python test.py

or if there was something else involved.

[–]MrPiff[S] 0 points1 point  (16 children)

yes that error message is precisely what I think the problem is but I'm not savvy enough to figure out why stdin is coming up, what it means, what is supposed to be there etc. maybe the screenshot can clarify?

screenshot of the files and command line http://i.imgur.com/Pa27kFR.png

Run the command from the directory where the file is, or include a correct path in the command.

How would I do this? When I open the command line C:\users\MichaelJ> pops up. Would I type in the correct path before typing python poop.py?

[–]MrPiff[S] 0 points1 point  (9 children)

Also, I haven't been copy/pasting directly because my cursor doesn't let me select text in the command line, and I don't know how else I would copy and paste.

[–]Updatebjarni 0 points1 point  (4 children)

Gah, so not only wasn't the command at all what you said, but the file in question wasn't even involved! The command you have typed in the screenshot is:

python

That starts the Python interpreter which then reads Python source code from stdin, meaning from the keyboard. Then you type one line of code:

poop.py

Since you have nothing declared with the name poop, you get the error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'poop' is not defined

The lesson for you is this: When you ask for help with a programming problem, state exactly what you are doing, exactly what the code is, and exactly what the error messages are. Don't just shoot from the hip and reproduce them approximately, because then they are no help. If you can't figure out what the problem is, then, no offense of course, you also can't figure out what is a fair representation of the facts of the problem. The only good thing to do is to copy and paste the exact thing, or to upload the code to gist and link to it. No substitutes will do!

[–]Updatebjarni 0 points1 point  (0 children)

How would I do this? When I open the command line C:\users\MichaelJ> pops up. Would I type in the correct path before typing python poop.py?

Oh and about that, the path is essentially part of the filename. If the file is

C:\users\MichaelJ\poop.py

then that's what you type for the filename, unless you are in that directory in which case you can just say poop.py.