How to Install Python Packages on Raspberry Pi Easily
To install Python packages on Raspberry Pi, use the
pip command in the terminal, like pip install package_name. For Python 3, use pip3 install package_name to ensure the package installs for Python 3.Syntax
Use the pip tool to install Python packages. The basic syntax is:
pip install package_name- installs a package for Python 2 (if installed).pip3 install package_name- installs a package for Python 3, which is the default Python on Raspberry Pi OS.- You can add
--userto install packages only for your user without needing admin rights.
bash
pip3 install package_name
Example
This example shows how to install the popular requests package for Python 3 on Raspberry Pi.
bash
pip3 install requests
Output
Collecting requests
Downloading requests-2.31.0-py3-none-any.whl (62 kB)
Installing collected packages: requests
Successfully installed requests-2.31.0
Common Pitfalls
Some common mistakes when installing Python packages on Raspberry Pi include:
- Using
pipinstead ofpip3when Python 3 is needed, causing packages to install for Python 2 or fail. - Not having
pip3installed. You can install it withsudo apt update && sudo apt install python3-pip. - Trying to install packages without
sudowhen system-wide installation is needed, leading to permission errors. - Not updating
pipitself, which can cause installation errors. Update withpip3 install --upgrade pip.
bash
Wrong: pip install requests Right: pip3 install requests
Quick Reference
| Command | Description |
|---|---|
| pip3 install package_name | Install a package for Python 3 |
| pip3 install --user package_name | Install package for current user only |
| sudo apt update && sudo apt install python3-pip | Install pip3 if missing |
| pip3 install --upgrade pip | Update pip to latest version |
| pip3 uninstall package_name | Remove a package |
Key Takeaways
Use
pip3 to install Python packages for Python 3 on Raspberry Pi.Install
python3-pip via apt if pip3 is not available.Use
sudo for system-wide installs or --user for user-only installs.Keep
pip updated to avoid installation issues.Check Python version and pip version to ensure correct package installation.