If you're using any modern Linux distro, you probably have seen the following message.

The message indicates that you should install python packages using your distro's package manager instead of using pip.

But what if the package is not available through your package manager, should you use — break-system-packages?

I would advise using this flag since, as indicated by the flag itself, it might break some of the packages installed via the distro's package manager.

Now the question is how you should do it, well there are two ways for doing this, either using pipx or a virtual environment.

Method 1 : using pipx

pipx is a tool that is similar to pip with a small yet very important difference, it installs every tool in its own virtual environment, this prevents any conflicts with other packages including system packages.

Using pipx

pipx usage is straight forward, for example, instead of using pip install scrapy you can use pipx install scrapy and this will install the package in its own environment, however, this sometimes can lead to some problems, for example what if I want to install another package in the same virtual environment, this happened to me when I wanted to install a plugin for a package I installed, if I use pipx directly it will be installed on a separate environment, so how I add that to the pre-existing one.

Adding packages to a pre-existing pipx environment

Actually there are two ways for doing this, using the pipx inject <package> <dependencies> or using the pipx runpip <package> <pip arguments> as you can see the first argument is the package name and the second is the package you want to add, for example the following are the commands to install bandit a static analysis tool for python and its sarif plugin.

# Installing the package itself.
pipx install bandit
# Adding the extra package bandit-sarif-formatter
# to the bandit environment.
pipx inject bandit bandit-sarif-formatter
None

Another option would be to use the pipx runpip command as seen below

# Same as before.
pipx install bandit
# Adding the extra package using run pip.
None

Method 2 : using virtual environment

Another way to avoid using --break-system-packages is to use a virtual environment, the advantage for this method is that you can choose the folder you install the packages in, this is useful for packages with big size (like AI related packages for example).

To create an environment and use pip in it, use the following commands

# Create a directory for the package.
mkdir bandit;cd bandit
# Create the virtual environemnt.
python3 -m venv .venv
# Activate it.
source .venv/bin/activate
# Run pip normally.
pip install bandit
pip install bandit-sarif-formatter

First, we create a folder to host our environment, then we activate it (you will need to repeat this step if you close your terminal).

Then you use pip as usual, the difference is that since you "sourced" the activate script, the packages will be installed in the virtual environment instead of installing them in the default python packages directory.

I hope this helps, see you soon in another blog post!!