dragonstar57 Posted January 31, 2011 Posted January 31, 2011 every time i type $python myfirstprogram.py it says syntaxerror: invalid syntax whats wrong with it?
dragonstar57 Posted February 1, 2011 Author Posted February 1, 2011 You'd have to show us your program. the program runs fine if you click to start it it just wont start the way the guide says it should http://openbookproject.net/thinkcs/python/english2e/ch01.html the code is print 1+1 could it have to do with the folder that it is in? maybe a more specific file path would work?
Cap'n Refsmmat Posted February 1, 2011 Posted February 1, 2011 Can you copy and paste the full error here? It should give details.
dragonstar57 Posted February 1, 2011 Author Posted February 1, 2011 Can you copy and paste the full error here? It should give details. not coped (it wont let me) >>> $python myfirstprograp.py $python myfirstprograp.py ^ SyntaxError: Invalid syntax
Cap'n Refsmmat Posted February 1, 2011 Posted February 1, 2011 Uhm. You don't tell the Python shell to start up Python to run a Python program. When it shows $ python, the $ indicates that that's a regular command prompt, not the Python REPL.
dragonstar57 Posted February 1, 2011 Author Posted February 1, 2011 Uhm. You don't tell the Python shell to start up Python to run a Python program. When it shows $ python, the $ indicates that that's a regular command prompt, not the Python REPL. so what is the correct code?
Cap'n Refsmmat Posted February 1, 2011 Posted February 1, 2011 "python myfirstprogram.py" is the correct code, but you need to run it in the plain command prompt, not in the Python REPL. In other words, don't go: $ python Python 2.5.1 (r251:54863, May 2 2007, 16:56:35) [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> $python myfirstprograp.py Do this: $ python myfirstprograp.py And the $ indicates the beginning of the command prompt line, not something you type in.
timo Posted February 1, 2011 Posted February 1, 2011 (edited) To make matters short (and not explaining what you did wrong - it only really matters if you are not using Windows, which seems rather unlikely). I'm not a python expert, but you could try import myfirstprograp.py or execfile("myfirstprograp.py") @Capn: My suspicion is that dragonstar might simply not have a console. Edited February 1, 2011 by timo
Cap'n Refsmmat Posted February 1, 2011 Posted February 1, 2011 Windows has Command Prompt, but execfile() should work too.
dragonstar57 Posted February 1, 2011 Author Posted February 1, 2011 (edited) python myfirstprogram.py SyntaxError invalid syntax python myfirstprogram.py ^ import myfirstprogram.py traceback <most recent call last?>: file"<stdin>", line 1, in <module> ImporteError: no module named myfirstprogram.py ececfile("myfirstprogram.py") SyntaxError: EOL while scanning string literal this is not working... :( Windows has Command Prompt, but execfile() should work too. To make matters short (and not explaining what you did wrong - it only really matters if you are not using Windows, which seems rather unlikely). I'm not a python expert, but you could try import myfirstprograp.py or execfile("myfirstprograp.py") @Capn: My suspicion is that dragonstar might simply not have a console. i'm using windows xp and the "program" that i am typing my code into is called python.exe Edited February 1, 2011 by dragonstar57
Cap'n Refsmmat Posted February 1, 2011 Posted February 1, 2011 Go to Start->All Programs->Accessories. Fire up Command Prompt. Save your Python program directly in C:/. In Command Prompt, type: "python.exe nameofyourpythonprogram.py" ececfile("myfirstprogram.py")SyntaxError: EOL while scanning string literal This just means it couldn't find the file.
timo Posted February 1, 2011 Posted February 1, 2011 (edited) Or add the line raw_input() as the last line of your program (it will wait for you to press <enter> before finishing the program), and try to run your program by double-clicking it. It you're lucky, files ending on ".py" are automatically opened with python. While Cap'n is right that Windows has a console, he's wrong in assuming that Windows users use it . Edited February 1, 2011 by timo
dragonstar57 Posted February 1, 2011 Author Posted February 1, 2011 (edited) Go to Start->All Programs->Accessories. Fire up Command Prompt. Save your Python program directly in C:/. In Command Prompt, type: "python.exe nameofyourpythonprogram.py" This just means it couldn't find the file. in quotes? Or add the line raw_input() as the last line of your program (it will wait for you to press <enter> before finishing the program), and try to run your program by double-clicking it. It you're lucky, files ending on ".py" are automatically opened with python. While Cap'n is right that Windows has a console, he's wrong in assuming that Windows users use it . (still not working) when i click them it opens and closes in under a second where does the rawinput command go? Edited February 1, 2011 by dragonstar57
Cap'n Refsmmat Posted February 1, 2011 Posted February 1, 2011 You'll have to be more specific, because I'm not psychic. What did you do, and what happened?
dragonstar57 Posted February 1, 2011 Author Posted February 1, 2011 (edited) You'll have to be more specific, because I'm not psychic. What did you do, and what happened? python.exe myfirstprogram.py file '<stdin>",line 1 python.exe myfirstprogram.py _____________________^ syntaxerror: invalid syntax Edited February 1, 2011 by dragonstar57
Cap'n Refsmmat Posted February 1, 2011 Posted February 1, 2011 python.exe myfirstprogram.py file '<stdin>",line 1 python.exe myfirstprogram.py _____________________^ syntaxerror: invalid syntax Are you running this in Command Prompt or in something else? Where are you typing this?
timo Posted February 1, 2011 Posted February 1, 2011 when i click them it opens and closes in under a second where does the rawinput command go? The raw_input() is supposed to stop exactly this immediate closing of your program. It should be the last command/line in the program file itself, i.e. a test program (stored in a file ending on ".py") could be print "Hello Windows ", 3+4 raw_input("Hit <enter> to ->exit") If that doesn't do the trick, then you'll either have to play around with some Windows settings (there might be something like "close window after program finishes") or learn how to use the Windows console.
Xittenn Posted February 2, 2011 Posted February 2, 2011 (edited) replace the execfile( "c:/Users/PrettyFlower/Desktop/myfirstprogram.py" ) with exec( open( "c:/Users/PrettyFlower/Desktop/myfirstprogram.py" ).read() ) and raw_input() with input() as you are using 3.1 and not 2 something :/ google errors as you will have more errors than you will be able to post eg. google => execfile nameerror name .... This is assuming that you are running in All Programs => Python 3.1 => Python (command line) myfirstprogram.py print( 1 + 1 ) input() I could probably expand on this and say that exec is acting on a 'stream' object. The file itself is not a 'stream object it is a file. So first we open the file with the open() function inputing the file name as parameter. This function loads the contents of the file as a 'storage' object. The 'storage' object is returned by reference whereupon the 'stream' object is created by appending the .read() to the reference returned by the open( fn ) function call. The changes themselves from release to release reflect optimizations done and the result closely mirrors the underlying function calls which are made via the python interpreter down to the c language. If you wish to run the program as the tutorial you linked suggested you would have to open All Programs => Accessories => command prompt and then go to the python directory so ... C:\python\ and run something like C:\python\python.exe myfirstprogram.py Edited February 2, 2011 by Xittenn
dragonstar57 Posted February 2, 2011 Author Posted February 2, 2011 (edited) i guess I this whole python is beyond me Edited February 2, 2011 by dragonstar57
Xittenn Posted February 2, 2011 Posted February 2, 2011 I wouldn't worry too much about the ramp up if it is something you really want to do. I've been doing this stuff since I was five and I still suck, but I do it anyway .... We don't all have to be Mark Zuckerberg. He's funny looking anyway /me shakes fist
dragonstar57 Posted February 2, 2011 Author Posted February 2, 2011 (edited) is the command prompt the same thing as a (command line) Or add the line raw_input() as the last line of your program (it will wait for you to press <enter> before finishing the program), and try to run your program by double-clicking it. It you're lucky, files ending on ".py" are automatically opened with python. While Cap'n is right that Windows has a console, he's wrong in assuming that Windows users use it . when i do that they close before i can see the output Or add the line raw_input() as the last line of your program (it will wait for you to press <enter> before finishing the program), and try to run your program by double-clicking it. It you're lucky, files ending on ".py" are automatically opened with python. While Cap'n is right that Windows has a console, he's wrong in assuming that Windows users use it . its still closing in before i can see if it works "python.exe nameofyourpythonprogram.py" was this meant to be in quotes?in the actual code? The raw_input() is supposed to stop exactly this immediate closing of your program. It should be the last command/line in the program file itself, i.e. a test program (stored in a file ending on ".py") could be print "Hello Windows ", 3+4 raw_input("Hit <enter> to ->exit") If that doesn't do the trick, then you'll either have to play around with some Windows settings (there might be something like "close window after program finishes") or learn how to use the Windows console. now the program runs when clicked replace the execfile( "c:/Users/PrettyFlower/Desktop/myfirstprogram.py" ) with exec( open( "c:/Users/PrettyFlower/Desktop/myfirstprogram.py" ).read() ) and raw_input() with input() as you are using 3.1 and not 2 something :/ google errors as you will have more errors than you will be able to post eg. google => execfile nameerror name .... This is assuming that you are running in All Programs => Python 3.1 => Python (command line) myfirstprogram.py print( 1 + 1 ) input() I could probably expand on this and say that exec is acting on a 'stream' object. The file itself is not a 'stream object it is a file. So first we open the file with the open() function inputing the file name as parameter. This function loads the contents of the file as a 'storage' object. The 'storage' object is returned by reference whereupon the 'stream' object is created by appending the .read() to the reference returned by the open( fn ) function call. The changes themselves from release to release reflect optimizations done and the result closely mirrors the underlying function calls which are made via the python interpreter down to the c language. If you wish to run the program as the tutorial you linked suggested you would have to open All Programs => Accessories => command prompt and then go to the python directory so ... C:\python\ and run something like C:\python\python.exe myfirstprogram.py C:\python\python.exe myfirstprogram.py file "<stdin>', line 1 C:\python\python.exe myfirstprogram.py ^ SyntaxError: invalid syntax Edited February 2, 2011 by dragonstar57
Cap'n Refsmmat Posted February 2, 2011 Posted February 2, 2011 Okay, tell me exactly what you're doing before you type in those commands. What program are you typing them into? How do you start it? Your problem seems to be that you're given the commands to the wrong thing, but you're not giving me enough detail to tell what you need to do.
dragonstar57 Posted February 2, 2011 Author Posted February 2, 2011 (edited) Okay, tell me exactly what you're doing before you type in those commands. What program are you typing them into? How do you start it? Your problem seems to be that you're given the commands to the wrong thing, but you're not giving me enough detail to tell what you need to do. sometimes i click the start menu and click on python(command line) that opens a window called python(command line) and sometimes i go to the C drive to click on the program and that oppens C:\python22\python.exe they both open identical windows except for the name(a small black window with white text in what appears to be python). i have the windows version http://wiki.python.org/moin/BeginnersGuide/Download Edited February 2, 2011 by dragonstar57
Cap'n Refsmmat Posted February 2, 2011 Posted February 2, 2011 Ah! That's your problem. Read this: http://docs.python.org/faq/windows#how-do-i-run-a-python-program-under-windows
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now