Installing Python using Homebrew
How to Install Python 3 on Mac Using Homebrew
Step 1: Install Homebrew
If you haven't installed Homebrew, open Terminal and paste the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Hit Return and wait for the installation to complete.
Step 2: Add Homebrew to Your PATH
Add Homebrew to your PATH by adding the appropriate line below to your ~/.profile or ~/.bash_profile file:
For macOS 10.12 (Sierra) or older:
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
For newer versions:
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
Step 3: Install Python 3 with Homebrew
In Terminal, run:
brew install python3
Homebrew will download and install Python 3.
Step 4: Verify Installation
Check the Python version:
python3 --version
If you see the version number, Python 3 is installed and ready to use.
Bonus Tip: Using Pip
Pip, Python's package manager, comes with Python 3 when installed via Homebrew. Install Python packages using the pip3
command to ensure you're working with Python 3 packages.
Configuring the PATH environment
Setting Python 3 as Default
- Open Terminal
- Locate Your Profile File: Depending on your macOS version, you'll be using either
.bash_profile
or.zshrc
in your Home directory. Create the file if it doesn't exist. - Edit the File: Open your profile file in a text editor.
- Add the Alias: Type
alias python='python3'
to set Python 3 as the default when 'python' is invoked. - Save and Close: Save your changes and close the editor.
- Activate Changes: In Terminal, type
source ~/.bash_profile
orsource ~/.zshrc
, depending on which file you edited. - Test: Type
python --version
in Terminal. You should see Python 3's version number.
By following these steps, you've set Python 3 as your default Python version. This change simplifies running Python scripts and ensures that you're using the Python version installed on your system.1
Working with Virtual Environments
Understanding Virtual Environments
A virtual environment is an isolated setup for your Python project, allowing it to have its own libraries and Python version separate from other projects. This separation prevents conflicts between different projects' dependencies and enables reproducibility.
Creating Your First Virtual Environment
- Open Terminal and navigate to your project directory:
cd path/to/your-project
- Run the following command, replacing
myprojectenv
with your desired name:python3 -m venv myprojectenv
Activating Your Virtual Environment
- Navigate into the virtual environment folder:
cd myprojectenv
- Activate the virtual environment:
source bin/activate
The command prompt will change to show the active environment.
Working in Your Virtual Environment
With the virtual environment active, you can install specific package versions:
pip install request==2.19.1
Your main system remains unaffected by the packages installed in the virtual environment.
Deactivating the Virtual Environment
Type deactivate
to exit the virtual environment. The command prompt will return to its normal state.
Virtual environments allow you to manage multiple Python projects with different requirements without interference.2 Use them to keep your projects organized and functional.
- Matthes E. Python Crash Course: A Hands-On, Project-Based Introduction to Programming. 2nd ed. No Starch Press; 2019.
- Sweigart A. Automate the Boring Stuff with Python: Practical Programming for Total Beginners. 2nd ed. No Starch Press; 2020.