Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Error : File "<stdin>"

Tags:

python-2.7

I'm trying to learn Python and am trying to execute a python file in terminal. I'm using 2.7.3 python version on my OS X. I've changed the directory in terminal to where the file is located, but I'm getting an error in terminal:

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

The ex1.py file contains:

print "Hello World!"
print "Hello Again"
print "I like typing this."
print "This is fun."
print 'Yay! Printing.'
print "I'd much rather you 'not'."
print 'I "said" do not touch this.'

Any ideas on how to fix this? Thx a bunch.

like image 975
Alina Tsui Avatar asked Nov 28 '22 07:11

Alina Tsui


2 Answers

>>> python ex1.py

You are trying to run your script from within a python interpreter. You don't want to do that.

Instead, just run that command in a terminal, not in the interpreter

$ python ex1.py

If you are still in the interpreter, you can press ctrl+d to leave it and return to the 'normal' terminal

like image 185
Tim Avatar answered Mar 11 '23 12:03

Tim


Exit python interpreter by typing exit() or press Crtl+Z

You will see the terminal now.

Type python file_name.py to run the code.

Happy coding!!!

like image 29
Decision Scientist Avatar answered Mar 11 '23 11:03

Decision Scientist