0

I tried running a simple python file from the command line. I created a python file called script0.py with the following content

 print 2**2

I get a syntax error.

>>> python script0.py
  File "<stdin>", line 1
    python script0.py
                 ^
SyntaxError: invalid syntax
>>>

What is that? My first try with python has been really bad and the book of Mark Lutz is not helping. I use Python 2.7.

Thanks

10
  • 4
    You're trying to issue a shell command to Python. python script0.py doesn't go in an interactive Python session; it goes in the shell, a.k.a. command prompt. Commented Mar 29, 2017 at 23:06
  • I have no idea; that works just fine for me. Do you have any leading spaces or other characters in the file? Commented Mar 29, 2017 at 23:06
  • @user2357112 is correct, you should post it as the answer Commented Mar 29, 2017 at 23:12
  • no, no leading spaces. How do I submit a simple python script that runs in the command prompt under windows? Commented Mar 29, 2017 at 23:14
  • I am using python 27 with Windows 10. When I simply type python I get errors too. Something is not right. Commented Mar 29, 2017 at 23:20

1 Answer 1

3

You are running that code from inside the python interpreter. If your terminal is showing a leading >>> it means your inside python already, in there you can either type print 2**2 directly, or exit and call python script0.py

My guess is that you inadvertently entered the python interpreter and thought you were still at the command prompt. Type exit() and try again.

demo

Sign up to request clarification or add additional context in comments.

5 Comments

Nope it is Python 2.7.
Thanks. exit() exits the command prompt. I use Windows 10. Probably Python 2.7.12 doesn't work with windows 10.
@nia: Protip: don't blame your tools until you understand them. Python works fine on Windows 10. You're just doing it wrong.
You need to do something like this C:\Python27\python.exe C:\Users\Username\Desktop\my_python_script.py changing the routes to those on your system
I was opening python interpreter in the command prompt. I just had to open a command prompt. Sorry for the stupid question and thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.