How to Use Python With Terminal on macOS
Yes, macOS can run Python from Terminal for quick experiments, scripts, and full development projects. The two main ways to use it are an interactive Python prompt for trying code line by line and .py files for programs you save and run again. Start by checking whether Python 3 is already available, then install it only if Terminal cannot find it.
Use this table to choose the workflow you need.
| Goal | Best Python Mode | What You Work In | Best For |
|---|---|---|---|
| Test an expression or learn syntax | Interactive REPL | The >>> Python prompt |
Small experiments and quick checks |
| Write a reusable program | Python script | A .py source file |
Automation, projects, and assignments |
| Keep one project’s packages separate | Virtual environment | A .venv folder in the project |
Applications with dependencies |
How Terminal and Python Work on a Mac
Terminal is the app where you enter shell commands. Python is a separate program that Terminal can start.
On a current Mac, Terminal’s default shell is zsh, according to Apple’s Terminal guide. A shell reads commands such as changing folders or launching programs. You do not need to replace zsh to use Python.
When you start Python, the prompt changes to >>>. That prompt means you are inside Python’s interactive interpreter, also called a REPL: read, evaluate, print, loop. Python runs each complete statement immediately and shows its result.
The Terminal shell runs programs; the Python REPL runs Python statements. Knowing which prompt you are looking at prevents many beginner mistakes, such as trying to use a shell command at >>> or typing Python code at the normal Terminal prompt.
How to Use Python From Terminal on macOS
Terminal lets you check, install, run, and save Python 3 programs on macOS.
Step 1: Open Terminal
- Open the Terminal app on your Mac.
- A new Terminal window opens with a prompt where you can type commands.
- Do not worry if the prompt includes your Mac name, username, or a
%character. It is simplyzshwaiting for a command.
Step 2: Check Whether Python 3 Is Installed
At the Terminal prompt, run this command:
python3 --version
If Terminal prints a Python 3 version, Python is ready to use.
If Terminal says command not found, install Python before continuing. Do not assume that the command python is the right interpreter: it can be missing or can refer to a different Python installation. Using python3 makes it clear that you intend to run Python 3.
Step 3: Install Python If Terminal Cannot Find It
macOS 12.3 removed the bundled Python 2.7 installation, so newer Macs may not include a usable Python command by default.
- Download the current macOS installer from Python.org Downloads.
- Open the downloaded
.pkginstaller and complete the installation. - In Finder, open Finder > Applications > Python 3.x, replacing
3.xwith the version that was installed. - Open
Update Shell Profile.commandfrom that folder. If macOS asks for confirmation, use the option to open it. - Quit Terminal, open a new Terminal window, and check Python again.
python3 --version
The shell-profile updater makes the installed python3 command available in new Terminal sessions.
Step 4: Run Python Interactively
Start the interactive Python interpreter with:
python3
You should now see a Python banner followed by the >>> prompt. Type a Python expression or statement and press Return:
print("Hello from Python")
Python should immediately print:
Hello from Python
This mode is useful for testing syntax, examining values, and learning the language. It is not the best place to write a longer program because your work is not automatically saved as a file.
To leave the Python prompt and return to Terminal, press Control-D. You can also enter:
exit()
Once you are back at the normal Terminal prompt, you can run shell commands again.
Step 5: Save and Run a Python Script
For code you want to keep, create a file ending in .py.
- Create a folder named
python-terminal-demoin your Documents folder. - Open that folder in a code editor or plain-text editor.
- Create a file named
hello.py. - Add this code and save the file:
print("Hello from a Python file")
- Return to Terminal and run this command to move into the folder:
cd ~/Documents/python-terminal-demo
- Run the saved script from that folder with Python 3:
python3 hello.py
Terminal should display:
Hello from a Python file
A script runs from top to bottom and then returns you to the normal Terminal prompt. If your script is in another folder, you can type python3, add a space, then drag the script file from Finder into Terminal to insert its full path before pressing Return.
Fix Python Not Showing Up in Mac Terminal
Python 3 Is Not Found After Installation
First, close every open Terminal window and open a new one. Terminal reads its startup configuration when a new shell begins, so an already-open window may not recognize a Python installation that was just added.
Then check again:
python3 --version
If it still fails, open Finder > Applications > Python 3.x and run Update Shell Profile.command again. Open another new Terminal window afterward.
Find Which Python Terminal Is Using
If python3 works but you are unsure where it came from, run:
whereis python3
This lists locations associated with the python3 command. It is useful when you have installed Python more than once or use developer tools that include their own Python copy.
Follow it with a version check:
python3 --version
The location tells you which command Terminal finds; the version confirms that it is Python 3.
The python Command Does Not Work
That is normal on many Macs. Use:
python3
Some systems provide python as an alias or as another interpreter, but python3 is the dependable command to use when you want Python 3. Avoid changing shell aliases just to make python work until you understand which interpreter and projects the alias would affect.
Use a Virtual Environment for Each Project
A virtual environment gives one project its own Python packages. This prevents a package upgrade for one project from unexpectedly changing another project.
From your project folder, create an environment named .venv:
python3 -m venv .venv
Activate it in the current Terminal window:
source .venv/bin/activate
After activation, commands such as python3 and pip use the environment’s interpreter and packages rather than your general Python installation. Check that its package installer is available:
python3 -m pip --version
Install only the packages your project needs. For example, if a project requires the requests library:
python3 -m pip install requests
When you are finished working in that project, leave the environment:
deactivate
I prefer a clean virtual environment for a project because it keeps package versions separate from the rest of the Mac.
Confirm Python Is Working Correctly
Use this short checklist after installation or when setting up a new project:
python3 --versionprints a Python 3 version rather than an error.- Starting
python3shows the>>>prompt. - Entering a simple
print()statement displays its text. - Pressing
Control-Dreturns from Python to the Terminal shell. - Running
python3 hello.pyprints the output in your saved script. - If you use a virtual environment, activate it before installing or running project-specific dependencies.
If these checks work, your Mac is ready for Python scripts, command-line tools, and projects.
Frequently Asked Questions
Can Python run on macOS?
Yes. Python runs well on macOS through Terminal, editors, and development tools. macOS 12.3 removed the bundled Python 2.7 installation, so you may need to install a current Python 3 release before the python3 command is available.
Do I need to change zsh to use Python?
No. Terminal’s default shell is zsh, and it can launch Python directly with python3. Changing the shell is optional and does not make Python work better; keep the default unless you have a separate reason to use another shell.
How do I run Python code in Mac Terminal?
For a quick experiment, enter python3 at the Terminal prompt and write code at >>>. For a saved program, create a file such as hello.py, move Terminal into that file’s folder, and run python3 hello.py.
How do I exit Python in Terminal?
Press Control-D while the >>> prompt is visible to exit the Python REPL and return to Terminal. You can also enter exit() and press Return.
Is a Mac good for Python coding?
Yes. macOS includes a Unix-style Terminal, works well with Python virtual environments, and supports common editors, package tools, containers, and source-control workflows. Start with a .py file and a project-local .venv, then add packages only when your project needs them.
