How can I run python in terminal on a mac?

Question:

I’m extremely new to programming, in fact I’m trying to learn Python now as a first language. I found that Google has a python course and it said to download python 2 from python’s website, which I did, and I now realize that python was already installed on my mac by apple. Anyways, I also had to download some exercises from google, and attempt to run some code in terminal and this is where I’m running into trouble and could really use some insight.

First, the lesson said to simply type, python, into terminal to start coding in python (I don’t know if this is the way to say that, or if that just gives you the ability to run python code?)

Then, it said to type in: python hello.py

Which was supposed to return: Hello World

However, all I keep getting is :

SyntaxError: Invalid Syntax

And I don’t really know where to go from here, I was thinking maybe it’s due to me downloading python again when it was already installed, if it even was?

Asked By: nukenine

||

Answers:

python command will open a Python interpreter, where you can’t run the python hello.py command so you got the syntax error (to run hello.py in python shell you need execfile(hello.py)) to quit the Python interpreter, type ctrl+d and than python hello.py to execute your hello world script.

Answered By: farhawa

First, the lesson said to simply type, python, into terminal to start coding in python (I don’t know if this is the way to say that, or if that just gives you the ability to run python code?)

What happens when you type a program name into terminal is that it basically executes the program, and if it has a GUI, the GUI window will pop up onto the screen. If it only has a command-line interface (like python) the program will be executed inside the terminal.

In the case of running python, what it does is actually to load a python interpreter inside your terminal. If you notice, the command-line interface would have changed quite a bit (I am on Windows so I am not sure how it looks like exactly on Mac). So instead of something like

YourName@USER ~/Path/to/your/exercise/files
$ _

at the start of the line, you would have encountered the below instead:

# Bunch of text describing your python version and other things
>>> _

That is actually the python interpreter’s command line. You can only execute python codes here.

Then, it said to type in: python hello.py

Which was supposed to return: Hello World

However, all I keep getting is : SyntaxError: Invalid Syntax

I suspect when you are running the command python hello.py, you were still inside the python interpreter program, which is why it return the InvalidSyntax error, because it is not a valid python syntax.

What python hello.py does (if you were to run it in your terminal instead) is to execute the python interpreter, supplying your hello.py file as the code for the interpreter to interpret and execute. So it is as if you run python and entering the codes you wrote inside hello.py into the python interpreter. So,

I was thinking maybe it’s due to me downloading python again when it was already installed, if it even was?

nope, it was not. It was because of the reasons I explained above.

Hope it helps!

Edit

A little bit of extra info, to properly exit the python interpreter command line, you can type exit() or quit(). Ctrl+D works too, as a shortcut.

Answered By: digawp

type python into terminal. It will display what version of python you have installed then you can begin by typing:

print("hello world!") 

python = completed it mate

Answered By: pythongenius..not

The first thing is that your file can’t be named python hellow.py it should be named python_hellow.py.

And to run it:
On macOS: type: python python_hellow.py
On Windows: type: python3 python_hellow.py

Answered By: Rafael Borri
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.