Skip to content Skip to sidebar Skip to footer

"SyntaxError: Invalid Syntax" When Trying To Run A .py Script From IDLE In Command Prompt

I just started learning python. I created a simple .py file using the IDLE editor and I am trying to run it from the command prompt. However, every time it keeps giving me the 'Syn

Solution 1:

That's not a Python program, it's the log of an interactive (command prompt) session.

Instead, try entering the following in any text editor (e.g. notepad, notepad++), save it as C:\code\script2.py and then run it as you did:

import sys

print(sys.platform)

x="Spam!"
print(x*8)

print(2**100)

[EDIT] If you want to use Idle for this, click [File][New] to create a Python source code file, type in the above, save it and then run it as you did.

[EDIT2] Idle is and example of an Interactive Development Environment (IDE). Since you're new to programming: IDE's tend to obscure what's going on, although Idle isn't a severe case of this. So using a separate editor and running from the command line as you did is actually a good way to familiarize yourself with what's going on under the hood. This will pay off in many ways in the long run.


Solution 2:

As said before; This is not a python file...

Python 2.7.10 (default, Feb  7 2017, 00:08:15) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

That's the shell (which you get if you just type: python) where you can type commands like:

fruits = ["apple", "banana", "cherry"]
for x in fruits:
    print(x)

and this will instantly execute...

What you need to to is go on and create a script.py file, write your stuff in there and then execute it as: python script.py

Greeting Elias


Post a Comment for ""SyntaxError: Invalid Syntax" When Trying To Run A .py Script From IDLE In Command Prompt"