When I try to install one package such as flask in my Raspberry Pi by running following command in terminal :
pip install flask
Error is shown as below:
error: externally-managed-environment
× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
python3-xyz, where xyz is the package you are trying to
install.
If you wish to install a non-Debian-packaged Python package,
create a virtual environment using python3 -m venv path/to/venv.
Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
sure you have python3-full installed.
For more information visit http://rptl.io/venv
note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
The error message encountering indicates that the environment is managed in such a way that it restricts the installation of packages directly using pip
in the system-wide Python environment. One solution is to use apt for installation
sudo apt install python3-flask
However, not all packages can be available through apt
. Instead, creating a virtual environment is a good practice to manage dependencies without affecting the system Python installation. Here’s how to do it:
1. Install python3-venv
if not already installed:
sudo apt install python3-venv
2. Navigate to Your Home Directory:
cd ~
3. Create a virtual environment:
python3 -m venv myenv
myenv
is the name of the virtual environment. It can be replaced by any desired name.
4. Activate the virtual environment:
source myenv/bin/activate
The terminal shows:
(myenv) rpi@raspberrypi:~ $
I can then use pip to install packages within this virtual environment without any issues. However, I need to activate the virtual environment first before I can use these packages. To exit a virtual environment in Python, just type
deactivate