I’m a newcomer to python. I just downloaded the 2.x version and am trying to write a simple ‘Hello world’ program in an editor, and run it in the interpreter. This is my code (what i have in IDLE):
#!C:\Python27\python.exe
#Filename: helloworld.py
print('Hello World')
This is what I run in the interpreter:
python helloworld.py
And this it the response i get:
SyntaxError: invalid syntax
Anyone know whats going on here? Please help.
CLARIFICATION:
I should add that the program runs on IDLE’s Run Module command. It just does not work in Python’s command line interpreter.
Just a little googling and relooking at your error message made it clear why you are getting the error.
Are you by any chance running your program in the idle prompt? If it says
python helloworld.pyit means it is complaining about this line not any specific line within helloworld.py.Just come out from idle (what you are calling the Interpreter) and run form the command prompt. You would not get that offending error.
Apart from what other’s have said about the print statement, the shebang in your script is unnecessary (but not wrong) if you are running under windows. That line make sense in *nix like environment where the shell determines the interpreter from the first line of the script.
Just to summarize
If you see something like
This is wrong. You cannot invoke python within IDLE.
You need to be in command prompt to run it like
This is right
In fact you can run your original script without any modification. Just run it the right way
Note*** Some other’s might have said print statement is not a function in Python 2.7 so that particulat syntax is wrong. This is partly correct
You have actually not invoked the print function. You have just Enclosed the string with paranthesis and that is Syntactically correct.
If you have problem with setting up Python and Running on Windows, you may start looking at couple of things