Python is one of the most versatile and beginner-friendly programming languages today. On macOS, you already have access to a powerful development environment through the Terminal. Learning how to use Python via the Mac Terminal unlocks automation, scripting, and software development capabilities without relying on third-party tools. This guide walks you through setting up Python, writing your first script, managing environments, and automating tasks—all from the command line.
Understanding Your Mac’s Built-in Python Environment
macOS comes with Python pre-installed, but the version varies depending on your operating system. Older versions of macOS shipped with Python 2.7, while newer systems include Python 3 or allow easy access through the App Store or Xcode command-line tools. However, relying on the system Python is not recommended for development due to potential conflicts with system processes.
To check what version of Python is available, open Terminal (found in Applications > Utilities) and type:
python3 --version
If Python 3 is installed, you’ll see output like Python 3.9.6 or similar. If not, you’ll need to install it—preferably using a package manager like Homebrew, which simplifies dependency management.
python3 instead of
python on macOS to avoid accidentally invoking the deprecated Python 2 interpreter.
Installing Python via Homebrew: The Developer’s Choice
Homebrew is a popular package manager for macOS that streamlines software installation. It ensures you get the latest stable version of Python and keeps it updated alongside other developer tools.
Step-by-Step Installation
- Open Terminal.
- Install Homebrew by running:
/bin/bash -c \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\"
- Once installed, update the package list:
brew update
- Install Python 3:
brew install python
- Verify the installation:
python3 --version
This method installs both python3 and pip3, the package installer for Python, enabling you to download libraries like requests, pandas, or flask.
Writing and Running Your First Python Script
Now that Python is properly installed, you can begin writing scripts. A script is simply a text file containing Python code saved with a .py extension.
Creating a Simple Script
- Navigate to your desired directory. For example:
cd ~/Documents/python-projects
- Create a new Python file using the
touchcommand:touch hello.py
- Edit the file using a command-line editor like
nano:nano hello.py
- Type the following code:
print(\"Hello, World!\") name = input(\"What's your name? \") print(f\"Nice to meet you, {name}!\") - Save and exit: Press Ctrl+O, then Enter, then Ctrl+X.
- Run the script:
python3 hello.py
You should see the program execute, print the greeting, prompt for your name, and respond accordingly. This confirms your environment is working correctly.
Managing Virtual Environments for Project Isolation
As you work on multiple projects, dependencies can conflict. For instance, one project may require version 2.0 of a library, while another needs version 3.0. Virtual environments solve this by creating isolated spaces for each project.
Creating a Virtual Environment
- Navigate to your project folder:
cd ~/Documents/my-project
- Create a virtual environment named
venv:python3 -m venv venv
- Activate it:
source venv/bin/activate
- Your prompt should now show
(venv)at the beginning, indicating the environment is active. - Install packages safely within this space:
pip install requests
- When done, deactivate:
deactivate
venv/ to your
.gitignore file if using version control—never commit virtual environments to repositories.
Essential Tools and Best Practices
Beyond basic scripting, mastering the Terminal involves adopting efficient workflows. Here are key practices every beginner should adopt.
Checklist: Python Development Setup on Mac
- ✅ Verify Python 3 is installed (
python3 --version) - ✅ Install Homebrew for easier package management
- ✅ Use
pip3to install required libraries - ✅ Create virtual environments for each project
- ✅ Use
nano,vim, or external editors like VS Code for coding - ✅ Keep scripts organized in dedicated folders
- ✅ Regularly update packages with
pip install --upgrade package_name
Common Pitfalls and How to Avoid Them
| Issue | Why It Happens | Solution |
|---|---|---|
command not found: python3 |
Python not installed or not in PATH | Reinstall via Homebrew and verify path with which python3 |
| Packages install globally despite virtual environment | Environment not activated | Always run source venv/bin/activate before installing |
| Permission errors when using pip | Using sudo unnecessarily |
Avoid sudo pip; use virtual environments instead |
“Treating your Terminal as a first-class development environment separates casual learners from effective coders.” — Laura Chen, Software Engineer & Educator
Real-World Example: Automating File Organization
Imagine you download dozens of files daily—PDFs, images, spreadsheets—and manually sorting them is time-consuming. Python can automate this.
Create a script called organize_downloads.py:
import os
import shutil
download_dir = \"/Users/yourusername/Downloads\"
file_types = {
\"Images\": [\".jpg\", \".jpeg\", \".png\", \".gif\"],
\"Documents\": [\".pdf\", \".docx\", \".txt\"],
\"Spreadsheets\": [\".xlsx\", \".csv\"]
}
for filename in os.listdir(download_dir):
filepath = os.path.join(download_dir, filename)
if os.path.isfile(filepath):
for folder, extensions in file_types.items():
if any(filename.lower().endswith(ext) for ext in extensions):
dest_folder = os.path.join(download_dir, folder)
os.makedirs(dest_folder, exist_ok=True)
shutil.move(filepath, os.path.join(dest_folder, filename))
Run it weekly via Terminal:
python3 organize_downloads.py
This reduces clutter and demonstrates how simple scripts can yield significant productivity gains.
Frequently Asked Questions
Can I use the default Python that comes with macOS?
You can, but it's risky. System Python is used by macOS for internal operations. Modifying it could destabilize your system. Always use a separate installation via Homebrew or the official Python website.
How do I upgrade pip on Mac?
Run:
python3 -m pip install --upgrade pipThis ensures you’re using the latest version of the package installer, improving compatibility and security.
Is it safe to run Python scripts from the internet?
No. Only run scripts from trusted sources. Malicious code can delete files, steal data, or install malware. Always review code before executing it.
Moving Forward: From Scripts to Solutions
Mastering Python on the Mac Terminal isn’t just about typing commands—it’s about gaining control over your computer. Whether automating repetitive tasks, parsing data, or building web backends, the skills you develop here form the foundation of modern software craftsmanship. Start small, experiment often, and gradually expand your scripts’ complexity.








浙公网安备
33010002000092号
浙B2-20120091-4
Comments
No comments yet. Why don't you start the discussion?