Python has become one of the most popular programming languages in the world with its incredible versatility. Whether you plan to build a website or an application yourself in Python, or want to run software that requires Python, you may need to install Python on your Mac.
This guide will discuss three of the most popular ways to get Python on your Mac and briefly introduce some alternatives.
Unfortunately, since macOS Catalina, Mac computers no longer have Python pre-installed, although it is included in older versions of the OS, including Mojave and earlier. If you want to use Python on your Mac today, you need to set it up and manage it yourself.
Even if you are running an OSX version containing Python, it may be an old version now—especially if you are not deliberately manually updating it. Given this, it's better to make sure you're running the latest version before you start using it.
There is an easy way to check if Python is installed on your Mac:
python3
and press Enter. If you have Python installed on your Mac, you will see the version number. If not, you will receive an error message.
You can get Python on your Mac in a number of different ways, some of which are simpler than others, and all work on Apple silicon chips (M1, M2, M3, and M4 chips) and Intel machines.
The following are three common methods that do not require much effort:
Choose the right installation method depends on your needs. If you only need Python to run programs and utilities that depend on it, an easy way to use the official Python installer should be great for you. However, if you are developing in Python in multiple environments, the Rye option is ideal.
Before proceeding, if you already have an older version installed, you may need to uninstall Python from your Mac. If you do not set up a virtual environment for each required version, installing a different version can lead to conflicts, which is actually only necessary for more complex Python projects.
Installing Python on your Mac using the official installer is one of the fastest ways to get it up and running, and if you just want to run Python programs and tools, this may be all you need. Just follow the steps below:
First of all, you need the official Python installer:
After downloading of the Python installer is completed, you can run it:
After the Python installer is finished, a new Python folder will appear, containing the official IDE (integrated development environment) called IDLE, the Python launcher, and some text documents. This means that Python has been installed successfully, but you can verify the installation using the above steps.
If you use Python for programming, you may need more advanced settings, especially if you plan to write more complex projects. A good option is to install Python using Rye, which allows you to install multiple "toolchains" simultaneously, which are essentially different versions of Python.
The following is how to install Python on your Mac using Rye:
You can quickly download and install Rye using the curl command in the terminal. Please follow the steps below:
curl -sSf https://rye.astral.sh/get | bash
and press Enter. Press the y key to continue when prompted. Rye After completing the Python installation, you will be asked if you want to add Rye to the PATH via .profile. This is usually best on Linux, but on macOS, it is best to set PATH in .zprofile. So let's set the PATH manually as follows:
~/.zprofile
file for editing. In the terminal, type open -e ~/.zprofile
and press Enter. source "$HOME/.rye/env"
, and save and close the file. You need to close and reopen the terminal to start a new session for the changes to take effect. When Rye's PATH is set, the Python
or Python3
command will now use the Python version installed by Rye, which is exactly the expected result. To confirm that the PATH is correct, type the echo $PATH
command into the terminal and press Enter. Your directory should have *.rye/shims
at the beginning.
After completing the above steps, verify that Rye installation is simple:
rye --version
, and press Enter. The version number should be displayed. If you see an error, check that you entered the correct command and that you followed the installation steps correctly.
Verify that Python installation is also easy, as shown below:
python --version
and press Enter to display the Python version number. If you see "zsh: command not found error", check that you have set PATH correctly by repeating the above steps.
If you want to build a simpler project with Python, it's quite easy to install Python using Homebrew, as shown below:
If you are a software developer, you may have installed Xcode on your Mac, including a Python version. It is important that you don't delete it. Instead, we will use Homebrew to install the latest version. However, for security reasons, Anaconda and other third-party tools can be uninstalled from your MacBook.
One of the fastest ways to uninstall older versions of Python and other unwanted applications on your Mac is to use MacKeeper's Smart Uninstaller. It recognizes all software installed on your Mac and allows you to delete multiple programs and their associated files in just a few clicks.
The following is how to use it:
Before you can install Python with Homebrew, you need to make sure it is ready and updated:
brew update
command, and press Enter. brew doctor
and pressing Enter. If Homebrew is not installed, you will see the "zsh: command not found" error. To install it, copy and paste /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
into the terminal, press Enter and follow the on-screen steps.
Homebrew is ready and updated, we can now install Python:
brew install python
into the terminal and press Enter. After the installation process is completed, we can verify that Python is installed correctly:
brew list python
into the terminal and press Enter to view the list of installed files. Homebrew will update Python as needed, but you may want to prevent this from happening – especially with some projects relying on a specific version of Python. You can use pin to prevent automatic updates:
brew pin python
, and press Enter. brew info python
. You should see pinned
at the end of your Python version number. If you want to update or reinstall Python with Homebrew in the future, you can delete the pin by typing the brew unpin python
command.
To upgrade Python with Homebrew, just type brew upgrade python
into the terminal and press Enter. Note that we are using upgrade
instead of update
here, because the update
command is used for Homebrew itself.
Remember that some of your projects and programs may require a specific version of Python to work properly. It is recommended that you check these dependencies before performing an update to avoid breaking anything you need.
After installing Python with Homebrew, you need to manually set $PATH, which is normal in most installation methods. Here's how to do this:
open -e ~/.zprofile
to open the configuration file in TextEdit. export PATH="$(brew --prefix python)/libexec/bin:$PATH"
, and save and close it. Your Homebrew version of Python will now take precedence over any other Python installation, but remember to close and restart the terminal for the changes to take effect. To verify that Python is installed correctly using Homebrew, type which python
into the terminal and press Enter. You should see the Python version number and the PATH you set above.
Depending on how you want to use Python, other installation methods may be more appropriate. The methods we've covered in detail above are some of the most common methods that should suit most use cases, but if they don't meet your needs, you may want to consider installing Python with pyenv or Conda.
One of the big advantages of Python is that it is very flexible. No matter how you want to use it, you will find a setup method that perfectly matches your needs. For most Mac users, we recommend two options: the official Python installer for running Python applications, and for development using Rye.
If you need to remove Python programs and other unwanted software, check out MacKeeper's Smart Uninstaller. It can clear all unwanted applications and delete all associated files at once, while other uninstall methods usually leave these files behind.
The above is the detailed content of Install Python on Mac: What Is the Best Way to Do This on MacOS?. For more information, please follow other related articles on the PHP Chinese website!