DEV Community

Adwaith Rajesh
Adwaith Rajesh

Posted on

Prevent accidental package installation to system Python.

Ever happened to you, that one day you decided to start afresh Python project, and started to install all the packages, like pandas, NumPy, jupyter notebook, and then you realized that you've installed all of them in the system python rather than in a virtual environment. Yeah, you are not alone buddy.

Well, worry no more. Introducing pip-venv-sure. (I don't know what I was thinking when I named it.)

Installation and Setup

pip3 insall pip-venv-sure
alias pip3=pip3-venv-sure
Enter fullscreen mode Exit fullscreen mode

Recommended to place the alias command in your .bashrc.

Now if you try to install a package without an activated venv this happens.

Screenshot from 2021-11-04 09-02-36.png

But if you have an activated venv it works as normal.

Screenshot from 2021-11-04 09-05-50.png

But, there are times when you've to install packages to the system Python in that case you can prevent pip-venv-sure from checking for a venv using the --allow-no-venv flag.

pip3 install any-template --allow-no-venv
Enter fullscreen mode Exit fullscreen mode

Screenshot from 2021-11-04 09-10-23.png

Some other alternatives.

If you don't like to install the said package, there is a way around it.

You can set an environment variable like this

export PIP_REQUIRE_VIRTUALENV=true
Enter fullscreen mode Exit fullscreen mode

The problem with it Is you've to unset or set this to false when you want to install packages globally.

Well, that's all for today, and Thanks for reading.

Top comments (0)