
Installing Python on Mac
So you've got your Mac, and you're ready to dive into the world of Python! Getting Python up and running on macOS is a straightforward process, but with a few different methods available, it's worth knowing your options to choose the one that best fits your needs. Whether you're a complete beginner or looking to manage multiple versions for different projects, this guide will walk you through the most common and reliable ways to get Python installed on your machine.
Checking Your Current Python Version
Before you install anything, it's a good idea to check if Python is already on your system. macOS comes with a system version of Python pre-installed, but it's often an older version (like Python 2.7, which is now deprecated) and not suitable for modern development. Let's see what you have.
Open your Terminal application. You can find it in your Applications folder under Utilities, or simply use Spotlight (Cmd + Space) and type "Terminal". Once it's open, type:
python --version
You might see something like:
Python 2.7.18
This is the system Python that macOS includes for its own internal use. It's best not to modify or rely on this version. You can also check for Python 3 specifically:
python3 --version
If this command is not found, it means you don't have Python 3 installed yet. If it returns a version number, you might already have it, but it could be an older release. Let's proceed with installing or updating to the latest version.
Installation Methods
There are a few primary ways to install Python on your Mac. Each has its pros and cons, and your choice might depend on how you plan to use Python.
Using the Official Installer from python.org
The most straightforward method for most users is to download the official installer from the Python website. This method is great if you want a quick, simple setup without additional tools.
First, open your web browser and go to python.org/downloads. The site should automatically detect that you're on a Mac and offer you the latest version. Click the download button for the macOS 64-bit universal2 installer. This will download a .pkg file.
Once the download is complete, open the file. This will launch the Python installer. Follow the on-screen instructions. It's generally safe to use the default settings. The installer will put Python in your Applications folder and update your system PATH so you can run Python from the Terminal.
After the installation is complete, open a new Terminal window (important to refresh the environment) and verify:
python3 --version
You should see the version you just installed, for example:
Python 3.12.1
Method | Ease of Use | Flexibility | Best For |
---|---|---|---|
Official Installer | Very Easy | Low | Beginners, simple setups |
Homebrew | Easy | Medium | Developers, those using CLI tools |
Pyenv | Moderate | High | Advanced users managing multiple versions |
This method is perfect if you just need one version of Python for general scripting, learning, or data analysis. However, if you plan to work on multiple projects that require different Python versions, you might want to consider a version manager like pyenv
later on.
Installing Python with Homebrew
Homebrew is a hugely popular package manager for macOS. If you're a developer or plan to install many command-line tools, having Homebrew is incredibly useful. It makes installing and updating software like Python a breeze.
First, you need to install Homebrew itself if you haven't already. Paste the following command into your Terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Follow the prompts on the screen. It will ask for your password and explain what it will install. Once Homebrew is set up, you can install the latest version of Python with a single command:
brew install python
This will install the latest stable version of Python 3. Homebrew nicely avoids conflicts with the system Python. It installs the python3
command and also symlinks python
and pip
to their Python 3 versions. Verify the installation:
python3 --version
# and also check that 'python' now points to python3
python --version
Both commands should return the same version number. The main advantage of using Homebrew is that updating Python in the future is very simple:
brew update && brew upgrade python
Using Pyenv for Version Management
If your work requires you to switch between different versions of Python (e.g., you need to test your code on Python 3.8, 3.9, and 3.12), pyenv
is the tool for the job. It allows you to install and manage multiple Python versions seamlessly.
The easiest way to install pyenv
is through Homebrew. If you followed the previous section, you're all set. If not, install Homebrew first. Then, run:
brew install pyenv
Next, you need to add some configuration to your shell so pyenv
can work its magic. The commands depend on which shell you're using (zsh is the default on modern macOS). Add the following lines to your ~/.zshrc
file (or ~/.bash_profile
if you're using bash):
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
After saving the file, restart your shell or run source ~/.zshrc
to load the new configuration. Now you can see all available Python versions:
pyenv install --list
This will output a very long list. To install a specific version, say Python 3.11.6:
pyenv install 3.11.6
Once installed, you can set this version to be your global default:
pyenv global 3.11.6
Now, whenever you run python --version
, it will use this version. You can also set a local version for a specific project directory, which is incredibly powerful for maintaining project-specific environments.
cd my_project_folder
pyenv local 3.10.12
This creates a .python-version
file in the directory. Whenever you're in this directory, pyenv
will automatically switch to the specified version.
Verifying Your Installation and Running Your First Code
No matter which method you used, let's make sure everything is working correctly. Open your Terminal and run:
python3 --version
You should get a version number back. Now, let's start the Python interpreter interactively. Type:
python3
You should see the Python prompt appear, looking like >>>
. This is the interactive shell. Let's run a simple command:
print("Hello, World! Python is installed on my Mac!")
Press enter, and you should see the message printed back to you. To exit the interactive shell, you can type exit()
or press Ctrl-D
.
Now, let's create and run a simple script. Open a text editor (like TextEdit, but make sure to use plain text mode, or better yet, a code editor like VS Code) and create a new file. Type the following:
#!/usr/bin/env python3
for i in range(5):
print(f"This is loop iteration {i+1}")
Save this file on your Desktop as my_script.py
. Now, go back to your Terminal. Navigate to the Desktop and run the script:
cd ~/Desktop
python3 my_script.py
You should see the output:
This is loop iteration 1
This is loop iteration 2
This is loop iteration 3
This is loop iteration 4
This is loop iteration 5
Congratulations! You've successfully installed Python and run your first script.
Installing Packages with Pip
Python's real power comes from its vast ecosystem of third-party packages. These are installed using pip
, the package installer for Python. If you used the official installer or Homebrew, pip
should be available. Let's install a popular package, requests
, which is used for making HTTP requests.
In your Terminal, run:
pip3 install requests
You should see output showing the package being downloaded and installed. To test it, go back into the Python interactive shell:
python3
And try to import the library:
import requests
response = requests.get("https://httpbin.org/json")
print(response.status_code)
If it returns 200
, the installation was successful! You can now use the requests
library in your scripts.
- Always use
pip3
to avoid accidentally installing packages for Python 2. - It's considered best practice to use virtual environments for project-specific dependencies to avoid conflicts between projects.
- You can upgrade pip itself with the command:
pip3 install --upgrade pip
.
Troubleshooting Common Issues
Sometimes, things don't go perfectly smoothly. Here are a few common issues and how to solve them.
"Command not found: python3" after installation. This usually means your shell's PATH environment variable hasn't been updated. The easiest fix is to simply close your Terminal application and open a new window. This refreshes the environment. If it still doesn't work, the installer might not have added Python to your PATH. You can try reinstalling and carefully checking the installation steps.
Getting permission errors when using pip. If you see errors like "Permission denied" or "Could not install packages due to an EnvironmentError", it means you are trying to install packages system-wide without sufficient permissions. A much better solution than using sudo
is to use virtual environments, which we will discuss next. Using sudo
with pip can cause conflicts with your system's package manager and is generally not recommended.
Multiple Python versions causing confusion. If you've installed Python in multiple ways, you might have several python3
binaries. You can see where each one is located using the which
command:
which python3
which python
This will show you the file path of the executable that runs when you type the command. This can help you understand which installation is being used.
The Next Step: Using Virtual Environments
Once you start working on real projects, you'll quickly want to use virtual environments. A virtual environment is an isolated workspace for your project. It has its own copy of the Python interpreter and its own set of installed packages. This means you can have one project that uses Django 4.2 and another that uses Django 3.2 without them interfering with each other.
Creating a virtual environment is simple. Navigate to your project directory and run:
python3 -m venv my_project_env
This creates a folder called my_project_env
which contains the isolated environment. To start using it, you need to "activate" it:
source my_project_env/bin/activate
Once activated, your shell prompt will change, usually showing the name of the environment in parentheses. Now, any pip install
commands will install packages only into this environment, and the python
command will use the environment's interpreter.
(my_project_env) $ pip install requests # Installs only here
(my_project_env) $ python my_script.py # Uses this env's python
When you are done working, you can deactivate the environment:
deactivate
Your prompt will return to normal. Using virtual environments is a fundamental best practice in Python development.
Common Command | Action |
---|---|
python3 -m venv env_name |
Creates a new virtual environment |
source env_name/bin/activate |
Activates the environment (Linux/macOS) |
deactivate |
Exits the current virtual environment |
You now have a fully functional Python setup on your Mac! You've learned how to install it using different methods, run code, install packages, and are prepared to use virtual environments for your projects. The world of Python is now at your fingertips. Happy coding