Install Python Packages with Pip or Rye
How to install Pip or Rye on Mac. How to install Python packages. Compare package managers Python Pip and Rye.
Python is often the first programing language you'll install on a Mac. Until recently, a lack of standard development tooling made setting up Python complicated. So, Python installation guides are often out of date or confusing. This guide describes current best practices.
Install Pip or Rye?
Pip has been a popular choice for installing Python packages. But there's a new option for installing Python packages, an all-in-one tool named Rye. Rye is a one-stop version and package management solution. This guide shows how to install Pip or Rye so you can choose the better tool.
Before you get started
You'll need a terminal application to install Python packages. Apple includes the Mac terminal but I prefer Warp Terminal. Warp increases developer productivity, helping you remember easily-forgotten commands and adds AI troubleshooting. Download Warp Terminal now; it's FREE and worth a try.
Rye combines version management and package management in a single tool. If you choose to use Rye, just install the Rye tool from the command line before installing Python and packages.
If you want to use Pip, you'll first need to Install Homebrew, then Install Pyenv (a Python version manager), and then a Python version.
Steps
Here's how to install Python packages on Mac.
- Install Rye (recommended).
- Install Homebrew, Install Pyenv, and use Pip (not recommended).
First, we show how to install and use Rye. Then we show how to install Python with Pyenv and use Pip so you can compare the tools.
Python version managers
Version managers allow you to install Python versions and switch among them easily.
If you choose Rye, you'll have an all-in-one version manager and package manager. Alternatively, if you install Pyenv to manage Python versions, you'll use Pip for package management.
Python package managers
Package managers allow you to download, install, and update software libraries and their dependencies.
Python is popular because developers have written and shared many useful software libraries (“packages”) through the Python Package Index (PyPI). Any software project is a hierarchy of packages that provide basic functionality, alongside custom code that adds unique features for the specific application. Some packages enable connection to databases or APIs. Other packages make development more efficient, for example, packages for testing. You'll need a package manager to install, update, and remove software packages. Most packages depend on other external software libraries; the package manager will fetch and install any dependencies required by that package.
Pip is the standard package manager for Python, included with any version of Python since Python 3.3. Pip
specifies packages in a requirements.txt
file. Newer package managers use a pyproject.toml
file instead. Updating a package with pip
may not automatically update all of its relative dependencies which can lead to conflicts.
Alternatives to pip
include Rye, pipx, Pipenv, Conda, PDM, Poetry, and PyFlow (no longer maintained). Alternatives provide more robust dependency management, platform independence, and a better integrated workflow for Python package management. The blog post Python Has Too Many Package Managers provides a good overview.
In this article, we'll first show how to install and use Rye for package management. Then we'll show how to install and use Pip for package management so you can compare.
Check for Python
Our article Mac Python describes your options for installing Python.
Check to see if your Mac already has Python.
$ python --version
zsh: command not found: python
You'll see zsh: command not found: python
if Python is not available.
Try python3 --version
to check if Python was installed with Xcode Command Line Tools.
$ python3 --version
Python 3.9.6
$ which -a python3
/usr/bin/python3
You'll need to modify the .zprofile
file to set the Mac PATH after installing Python with Pyenv or Rye to make the newest version available.
Rye
Install Rye and Python
You can install Rye with a curl
command. Curl is a command-line tool that makes HTTP requests from the terminal, useful for tasks like downloading and running installation scripts.
$ curl -sSf https://rye.astral.sh/get | bash
This script will automatically download and install rye (latest) for you.
######################################################################## 100.0%
Welcome to Rye!
This installer will install rye to /Users/daniel/.rye
This path can be changed by exporting the RYE_HOME environment variable.
Details:
Rye Version: 0.26.0
Platform: macos (aarch64)
? Continue? (y/n)
Enter y
to continue. Rye will ask questions to customize the installation.
? Select the preferred package installer ›
❯ uv (fast, recommended)
pip-tools (slow, higher compatibility)
By default, Rye offers uv
, a faster and newer package installer. I recommend choosing pip-tools
for compatibility. If you're a beginner, it will be easier to follow tutorials that refer to pip
. Select pip-tools
with the arrow keys.
Next, the self-installer asks which Python version you'll use as a default, offering the Rye-installed version or previously-installed versions.
? What should running `python` or `python3` do when you are not inside a Rye managed project? ›
❯ Run a Python installed and managed by Rye
Run the old default Python (provided by your OS, pyenv, etc.)
It's best to use the Rye-installed version. Accept the default Run a Python installed and managed by Rye
by pressing "Enter." Then the self-installer asks which Python version to install as a default
? Which version of Python should be used as default toolchain? ([email protected]) ›
Accept the default and Rye will install the latest Python version. Installation begins when you press "Enter."
Installed binary to /Users/daniel/.rye/shims/rye
Bootstrapping rye internals
Downloading [email protected]
Checking checksum
Unpacking
Downloaded [email protected]
Upgrading pip
Installing internal dependencies
Updated self-python installation at /Users/daniel/.rye/self
The rye directory /Users/daniel/.rye/shims was not detected on PATH.
It is highly recommended that you add it.
? Should the installer add Rye to PATH via .profile? (y/n) ›
Notice that Rye installs its Python files to ~/.rye/shims/rye
.
Rye offers to set the $PATH
to give precedence to its Python version by modifying the .profile
file. Use of the .profile
file is a Linux convention. On the Mac, it's preferred to set the $PATH
in .zprofile
or .zshrc
files, preferably .zprofile
. Enter n
to skip this automatic step. Later, you'll set the $PATH
manually.
✔ Should the installer add Rye to PATH via .profile? · no
note: did not manipulate the path. To make it work, add this to your .profile manually:
source "$HOME/.rye/env"
To make it work with zsh, you might need to add this to your .zprofile:
source "$HOME/.rye/env"
For more information read https://rye.astral.sh/guide/installation/
All done!
Rye explains how to complete the installation manually by editing the .zprofile
file. I'll show you how do it.
Set the $PATH
for Rye
There's one final important step before Rye works correctly. You must set the Mac PATH to make sure Rye finds the correct Python version. Otherwise, entering the command python
will trigger zsh: command not found: python
and the command python3
will access the older Xcode-installed Python version.
You can use TextEdit, the default macOS graphical text editor, to edit the ~/.zprofile
file. Alternatively, you can edit the ~/.zshrc
file. It works the same but, by convention and design, the ~/.zprofile
file is used for setting the $PATH
(see .zshrc or .zprofile for an explanation). You can open a file in TextEdit from the terminal:
$ open -e ~/.zprofile
You also can use the command line editors nano
or vim
to edit the shell configuration files. See Shell Configuration for more about editing shell configuration files.
Add this command as the last line of your configuration file to configure the Z shell for Rye.
source "$HOME/.rye/env"
When your terminal session starts, Z shell will run the ~/.rye/env
script to set shims to intercept and redirect any Python commands. You'll need double quotes because the command contains spaces or special characters. Rye adds the shims to your $PATH
so that running the command python
or python3
will run a Rye-installed Python version.
Changes to the ~/.zprofile
file will not take effect in the Terminal until you've quit and restarted the terminal. Alternatively (this is easier), you can use the source
command to reset the shell environment:
$ source ~/.zprofile # Or just restart your terminal
The source
command reads and executes a shell script file, in this case resetting the shell environment with your new $PATH
setting.
After resetting your shell, you can check the $PATH
setting.
$ echo $PATH
/Users/daniel/.rye/shims:/opt/homebrew/bin:/opt/homebrew/sbin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin
The ~/.rye/shims
directory should be leftmost, taking precedence over other directories.
Verify Rye installation
After installing Rye, use rye --version
to verify that it has been installed.
$ rye --version
rye 0.26.0
commit: 0.26.0 (d245f625e 2024-02-23)
platform: macos (aarch64)
self-python: [email protected]
symlink support: true
uv enabled: false
Verify Python installation
Check that Python is available:
$ python --version
Python 3.12.1
If you see zsh: command not found: python
, check that the Mac PATH is set correctly.
The python3
command should give you the Rye-installed version, not the Xcode-installed version:
$ python3 --version
Python 3.12.1
The which
command shows the Rye shims directory when you try to see where Python is installed. Keep in mind that you've set the ~/.zprofile
file to use Rye shims to intercept the python
command and deliver the Rye-installed versions.
$ which python
/Users/daniel/.rye/shims/python
You've successfully installed Python with Rye. Rye's advantage over Pyenv is an integrated environment manager and package manager. With Pyenv, you'll need to use the Pip package manager.
Let's see how to install Python packages with Rye.
Install a Python package with Rye
Package managers allow you to download, install, and update software libraries and their dependencies. Most packages depend on other external software libraries; the package manager will fetch and install any dependencies required by that package.
Before you try to install a package with Rye, be sure you've created a pyproject.toml
file in your project root directory with rye init
.
You can install any Python package from the Python Package Index. Here we'll install the cowsay utility.
$ rye add cowsay
Added cowsay>=6.1 as regular dependency
If you see error: did not find pyproject.toml
, you need to run rye init
.
Rye sync
Before you can use a package in a Rye project, you must run rye sync
to update lockfiles and install the dependencies into the virtual environment.
$ rye sync
Reusing already existing virtualenv
Generating production lockfile: /Users/daniel/workspace/myproject/requirements.lock
Creating virtualenv for pip-tools
Generating dev lockfile: /Users/daniel/workspace/myproject/requirements-dev.lock
Installing dependencies
Looking in indexes: https://pypi.org/simple/
Obtaining file:///. (from -r /var/folders/ls/g23m524x5jbg401p12rctz7m0000gn/T/tmpyzjrebff (line 2))
Installing build dependencies ... done
Checking if build backend supports build_editable ... done
Getting requirements to build editable ... done
Preparing editable metadata (pyproject.toml) ... done
Collecting cowsay==6.1 (from -r /var/folders/ls/g23m524x5jbg401p12rctz7m0000gn/T/tmpyzjrebff (line 1))
Downloading cowsay-6.1-py3-none-any.whl.metadata (5.6 kB)
Downloading cowsay-6.1-py3-none-any.whl (25 kB)
Building wheels for collected packages: myproject
Building editable for myproject (pyproject.toml) ... done
Created wheel for myproject: filename=myproject-0.1.0-py3-none-any.whl size=1066 sha256=13905ba3ed5b7de2a39c0eedea6a9be9e7d8a7750aa602a4ea78c27da3f9dfe8
Stored in directory: /private/var/folders/ls/g23m524x5jbg401p12rctz7m0000gn/T/pip-ephem-wheel-cache-pkuzm62h/wheels/8b/19/c8/73a63a20645e0f1ed9aae9dd5d459f0f7ad2332bb27cba6c0f
Successfully built myproject
Installing collected packages: myproject, cowsay
Successfully installed cowsay-6.1 myproject-0.1.0
Done!
Rye displays all its operations but you don't have to read anything.
Run Python
After installing a package and running rye sync
, you can use the Python interpreter interactively (the REPL or Read-Eval-Print Loop).
$ python
Python 3.12.1 (main, Jan 7 2024, 23:31:12) [Clang 16.0.3 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cowsay
>>> cowsay.cow('Hello World')
___________
| Hello World |
===========
\
\
^__^
(oo)\_______
(__)\ )\/\
||----w |
|| ||
>>>
Enter quit()
or type Control + D
to exit the Python interpreter.
If you like the cowsay package, try dinosay. Now that you've installed Rye, you can install any Python packages you like.
Uninstall Rye
Here's how to remove Rye, if you decide not to use it.
$ rye self uninstall
✔ Do you want to uninstall rye? · yes
Done!
Don't forget to remove the sourcing of $HOME/.rye/env from your shell config.
Rye will uninstall itself but you will need to delete the ~/.rye/
hidden directory in your user home directory.
$ rm -rf ~/.rye/
Finally, use a text editor to remove this line from your ~/.zprofile
file:
source "$HOME/.rye/env"
You've uninstalled Rye.
Pip
For comparison, you can install Pyenv and use Pip as a package manager.
Pip is the standard package manager for Python, included with any version of Python since Python 3.3. If you've used Node to run JavaScript, pip
is similar to npm
, the Node package manager. However, npm install
installs packages within a project by default, whereas pip install
installs packages into a system Python or shared Python versions, creating potential conflicts. To install Python packages for a specific project with pip
, it's best to use a Python environment manager such as Venv to create and activate a virtual environment. You'll avoid dependency conflicts when you have multiple projects that require different versions of the same packages.
Install Pyenv to use Pip
Start by installing Pyenv using Homebrew.
After you've installed Pyenv and a current Python version, you can use Pip to install Python packages.
Pip is included with Python
Since Python 3.3, Python installations come with pip
by default.
Check that Pip is available:
$ pip --version
pip 24.0 from /Users/daniel/.pyenv/versions/3.12.2/lib/python3.12/site-packages/pip (python 3.12)
If you've installed Pyenv, the which
command will show that Pyenv shims are intercepting calls to pip
.
$ which pip
/Users/daniel/.pyenv/shims/pip
Install a Python package
You can install any Python package from the Python Package Index. Here we'll install the cowsay utility.
$ pip install cowsay
Collecting cowsay
Using cached cowsay-6.1-py3-none-any.whl.metadata (5.6 kB)
Using cached cowsay-6.1-py3-none-any.whl (25 kB)
Installing collected packages: cowsay
Successfully installed cowsay-6.1
Recent versions of pip
implement PEP 668 to prevent attempts to install a package into a system Python. Using Pyenv with pip
, you'll avoid an error: externally-managed-environment
that results from violating PEP 668 (see error: externally-managed-environment). However, Pyenv and pip
don't prevent clashes between packages required by different projects. You'll begin to encounter dependency conflicts if you have multiple projects that require different versions of the same packages.
With Pyenv, any package installed with Pip will be installed in a common folder for a specific Python version and shared with any other projects that use the same Python version. That's because Pyenv manages Python versions, not projects. You should install pyenv-virtualenv to isolate packages in different projects. Or use Rye for an all-in-one tool that takes a project-oriented approach.
Run Python
After installing a package with Pip, you can use the Python interpreter interactively (the REPL or Read-Eval-Print Loop).
$ python
Python 3.12.2 (main, Mar 24 2024, 13:24:12) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cowsay
>>> cowsay.cow('Hello World')
___________
| Hello World |
===========
\
\
^__^
(oo)\_______
(__)\ )\/\
||----w |
|| ||
>>>
Enter quit()
or type Control + D
to exit the Python interpreter.
What's next
My mac.install.guide is a trusted source of installation guides for professional developers. Take a look at the Mac Install Guide home page for tips and trends and see what to install next.