info-nowise
info-nowise

Reputation: 61

Can't run Python from cmd line

I am a programming (and Python) novice. I am unable to run any python script in command prompt of my WinXP 64-bit laptop. I assigned the path and confirmed it by typing set path. I have Python32. I get the following message:

>>>python hello.py

File <”stdin”> , Line1
  Python hello.py
             ^
SyntaxError: Invalid syntax

Following is the script I tried:

#!/usr/bin/python
message = "Hello, world!"
print(message)

Upvotes: 5

Views: 40856

Answers (6)

Wiz
Wiz

Reputation: 4865

Another way of doing it inside the interpreter is by just importing the name of the module without the .py so for example, in your case:

>>>import hello

would return

Hello, World!

Upvotes: 1

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250881

Do it like this:

Go the directory(python32 in my example) and type python hello.py.

If you only type python in cmd then it'll launch the python interpreter after that python hello.py will return Syntax error.

enter image description here

Upvotes: 5

jdeuce
jdeuce

Reputation: 1866

You're inside the python interpreter not the windows command line.

To open the windows command line go to

Start -> All Programs -> Accessories -> Command Prompt

Then you will need to change into the directory where you have stored hello.py. If it was on your desktop you would do:

cd Desktop

and then after that you could do:

python hello.py

and it would work.

Upvotes: 0

tskuzzy
tskuzzy

Reputation: 36446

python is the command you use to run the script so you run it in the shell. In Windows, that would be the command prompt (Run > "cmd").

Upvotes: 0

Levon
Levon

Reputation: 143037

You are typing the command inside the Python shell, do it from the DOS prompt.

I.e.,

 C:\somepath\> python hello.py

since you already have the python executable on the path (the number 1 problem usually when things don't work), you should be set to go.

Upvotes: 0

John La Rooy
John La Rooy

Reputation: 304137

You should type this

python hello.py

at the dos/cmd prompt, not inside the Python Interpreter

Upvotes: 6

Related Questions