How can I run python in terminal on a mac?

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.

Leave a Comment