In this article, we cover how to fix an error we received while installing a package through PIP. We would like to add here that, it isn’t an issue. This was done to address the conflict that arises between the Python package management tools and the systems’ package manager.
In the past, the packages have been installed in a global context by Python package management tools. Instead, a more effective solution could be to manage and operate them from a virtual environment. The error we received isn’t an error. It just prompts to use a virtual environment while managing a package through a Python package management tool like PIP.
We can explore the topic further: PEP 668
[Fixed] PIP Error: externally-managed-environment
Option I. Install required packages from the standard Ubuntu repository. For instance, if you wish to install BeautifulSoup 4 then,
sudo apt update sudo apt install python3-bs4
Option II. Create a Virtual Environment.
If the package we wish to install isn’t available in the repository. Then, we can use a Virtual Environment. First, to install a package:
sudo apt update sudo apt install python3-venv
The virtual environment will have dedicated site directories.
Next, to create a virtual environment:
python3 -m venv /path/to/virtual/environment
For instance,
python3 -m venv ~/.virtualenv/test
Now, we run pip and python through:
/path/to/virtual/environment/bin/pip install bs4 /path/to/virtual/environment/bin/python3
Option III. Through a package, PIPX. It can be installed through:
sudo apt update sudo apt install pipx
It manages packages for us in an isolated virtual environments.
pipx install [package_name]
For instance,
pipx install mplfinance --include-deps
To view all the associated command-line options,
pipx -h
In conclusion, we have covered here how to install packages through PIP.