If you’re using macOS, you may have noticed that it comes with an outdated version of Python 2 installed by default. Fortunately, you can easily install the latest version of Python 3 on your Mac. In this blog post, we’ll walk you through the process step by step.
By installing Xcode, the Apple Development IDE, you will have Python 3 installed at /usr/bin/python3
. You can check the version of Python 3 installed by running python3
in your terminal. If the version is recent enough, then you’re all set.
Python 3 is the preferred version for modern Python development. However, keep in mind that Apple only updates the Python version with new Xcode releases, so your control over the version number you run may be limited.
To install the latest Python release, there are two options: using Homebrew or the official Python packages. In this guide, we’ll focus on using the official Python packages.
Here’s how you can install Python 3 on your Mac:
-
Go to the Python website (https://www.python.org) and click on the “Downloads” menu.
-
Move your cursor over “Mac OS X” and a panel will appear with a link to download the official Python package.
-
Click on the link and run the installer.
-
Click “Continue” to proceed with the installation.
-
Read the interesting recap on the history of Python and its governance.
-
Agree to the Python Software Foundation License Version 2.
-
Proceed with the installation.
-
Once the installation is complete, Python will be installed as
/usr/bin/python3
.
If you have Xcode installed, the newly installed Python version will overwrite the old one.
To verify that Python 3 is correctly installed, open the terminal and type python3
. The Python 3.9 interpreter should open.
After installing Python, you will also find a new folder under “/Applications/Python 3.9” (or your version number), containing some files. One of these files is “Install Certificates.command”, which you need to run to install the SSL certificates required by Python. This ensures that Python can securely communicate over HTTPS.
To run the “Install Certificates.command” file, simply double-click on it and let it do its job.
In addition, the Python folder contains IDLE, an application that opens the Python interpreter in its own window. You will also find a link to the official documentation, the license, and Python Launcher, a utility that allows you to manage different Python versions installed on your system.
It’s important to note that if you have the python
command on your system, it may still point to the old Python 2 version, which can cause problems. To avoid this, you can create a shell alias to execute python3
instead. Here’s how you can do it for the Fish shell:
alias python "python3"
alias pip "pip3"
funcsave python
funcsave pip
For Zsh, add the following alias to the .zshrc
file:
alias python="python3"
For Bash, add the following alias to the .bashrc
file:
alias python="python3"
After setting up the alias, you can simply run python
to use the Python 3 version you just installed.
When using the Python package manager, pip, it’s recommended to use python -m pip <COMMAND>
instead of pip <COMMAND>
. For example, to install Django, you would run:
python -m pip install django
And always remember to use a virtual environment with venv when working with Python packages.