Installing Python on macOS
macOS typically comes with Python pre-installed, but it’s often an older version (Python 2.x). To work with the latest version of Python (Python 3), it’s important to install Python 3 manually. This guide will walk you through downloading and installing Python 3 on macOS, setting up virtual environments, and verifying the installation.
1. Checking the Pre-installed Python
macOS comes with Python 2.x pre-installed, but it is recommended to use Python 3 for modern development. Before installing Python 3, check which version of Python is currently installed.
Step-by-Step Instructions:
-
Open Terminal:
- Press
Cmd + Space
to open Spotlight, type Terminal, and press Enter.
- Press
-
Check the Python Version:
- To check the pre-installed version of Python, run:
python --version
- This will likely show
Python 2.x.x
.
- To check the pre-installed version of Python, run:
-
Check for Python 3:
- You can also check if Python 3 is already installed by running:
python3 --version
- You can also check if Python 3 is already installed by running:
If Python 3 is not installed or you need to install a newer version, follow the steps below.
2. Installing Python on macOS
There are multiple ways to install Python 3 on macOS, but using Homebrew is the easiest and most common method.
2.1. Installing Homebrew (if not already installed)
Homebrew is a package manager for macOS that simplifies the installation of software. If you don't have Homebrew installed, you can install it by following these steps:
-
Open Terminal:
- Press
Cmd + Space
, type Terminal, and press Enter.
- Press
-
Install Homebrew:
- Run the following command to install Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Run the following command to install Homebrew:
-
Verify Homebrew Installation:
- Once installed, verify Homebrew by running:
brew --version
- Once installed, verify Homebrew by running:
If Homebrew is installed correctly, you'll see the installed version of Homebrew.
2.2. Installing Python 3 using Homebrew
Now that Homebrew is installed, follow these steps to install Python 3.
-
Update Homebrew:
- First, update Homebrew to ensure you have the latest version:
brew update
- First, update Homebrew to ensure you have the latest version:
-
Install Python 3:
- Run the following command to install Python 3:
brew install python
- Run the following command to install Python 3:
-
Verify Python Installation:
- After installation, verify that Python 3 was installed correctly by running:
python3 --version
- This should display the Python 3 version (e.g.,
Python 3.x.x
).
- After installation, verify that Python 3 was installed correctly by running:
-
Check pip Installation:
- pip, the Python package manager, should be installed automatically with Python. Verify this by running:
pip3 --version
- pip, the Python package manager, should be installed automatically with Python. Verify this by running:
3. Verifying the Installation
Once Python 3 is installed, it’s important to verify that the installation is working properly.
Step-by-Step Instructions:
-
Open Terminal:
- Open the Terminal application if it isn’t already open.
-
Check the Python 3 Version:
- To verify the Python 3 installation, run:
python3 --version
- This should return the version number of Python 3 (e.g.,
Python 3.x.x
).
- To verify the Python 3 installation, run:
-
Check pip Installation:
- pip is Python’s package manager, and it’s installed with Python 3. To check the version of pip, run:
pip3 --version
- This will show the installed version of pip.
- pip is Python’s package manager, and it’s installed with Python 3. To check the version of pip, run:
4. Setting Up a Virtual Environment
To manage dependencies for individual projects, it’s good practice to use virtual environments. Virtual environments isolate packages and dependencies for different projects, avoiding conflicts.
Step-by-Step Instructions:
-
Create a Virtual Environment:
- Use the built-in
venv
module to create a virtual environment. In the Terminal, navigate to the directory where you want to create your project, and run:python3 -m venv myenv
- This will create a new virtual environment called
myenv
.
- Use the built-in
-
Activate the Virtual Environment:
- To activate the virtual environment, use the following command:
source myenv/bin/activate
- Once activated, your terminal prompt will change to show
(myenv)
, indicating that the virtual environment is active.
- To activate the virtual environment, use the following command:
-
Deactivate the Virtual Environment:
- When you're done working in the virtual environment, deactivate it by running:
deactivate
- When you're done working in the virtual environment, deactivate it by running:
5. Installing Python Packages
Once Python is installed, you can install packages using pip, Python’s package manager.
Step-by-Step Instructions:
-
Installing a Package:
- To install a package, use the following command:
pip3 install package_name
- For example, to install the
requests
package:pip3 install requests
- To install a package, use the following command:
-
Verify Installed Packages:
- To see a list of installed packages, run:
pip3 list
- To see a list of installed packages, run:
6. Managing Multiple Python Versions (Optional)
If you need to manage multiple Python versions on macOS, you can use pyenv to switch between different versions of Python easily.
Installing pyenv on macOS
-
Install pyenv:
- You can install pyenv using Homebrew by running the following command:
brew install pyenv
- You can install pyenv using Homebrew by running the following command:
-
Add pyenv to Your Shell:
- Add the following lines to your shell configuration file (
~/.bash_profile
,~/.zshrc
, etc.) to make pyenv available:echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init --path)"' >> ~/.zshrc
- Add the following lines to your shell configuration file (
-
Install Multiple Python Versions:
- After pyenv is installed, you can install different versions of Python:
pyenv install 3.x.x
pyenv install 3.y.y
- After pyenv is installed, you can install different versions of Python:
-
Set Global or Local Python Version:
- To set the global Python version, run:
pyenv global 3.x.x
- You can also set a specific version for a project directory using:
pyenv local 3.y.y
- To set the global Python version, run:
Conclusion
You’ve successfully installed Python 3 on macOS, verified the installation, set up a virtual environment, and learned how to install Python packages. By managing Python versions and environments effectively, you're ready to start working on Python projects, including data science, web development, or automation.