Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent pip install without virtualenv?

Tags:

pip

virtualenv

I really hate running something like make and then be surprised by pip installing a load of packages because I forgot to activate my virtualenv.

Is there any way to force pip to prompt / warn me that I'm not in some virtualenv?

like image 390
Mikhail Avatar asked Dec 09 '14 21:12

Mikhail


People also ask

Do I need virtualenv?

There are also workflow tools that simplify this process, such as Pipenv and Poetry. They create virtual environments for you without perception and then install dependencies into them. They are used by a wide range of users.

Should I use VENV or virtualenv?

Traditionally virtualenv has been the library used to create virtual environments for python. However , starting python 3.3 , module venv has been added to python standard library and can be used as a drop-in replacement for virtualenv. If older version of python is being used, then virtualenv is the way to go.

Does pip install globally or locally?

If you're in an active virtual environment, then the command installs pip into that environment. Otherwise, it installs pip globally on your system.

Does virtualenv install pip?

Activating a virtual environmentAs long as your virtual environment is activated pip will install packages into that specific environment and you'll be able to import and use packages in your Python application.


2 Answers

Taken from http://docs.python-guide.org/en/latest/dev/pip-virtualenv/

You need to set a environmental variable PIP_REQUIRE_VIRTUALENV

Best practice would be to place it in your autostart file (.bash_profile or similar)

export PIP_REQUIRE_VIRTUALENV=true

To install the package globally, you can then run PIP_REQUIRE_VIRTUALENV="" pip ... or create a command gpip, also in the autostart file:

gpip() {
    PIP_REQUIRE_VIRTUALENV="" pip "$@"
}
like image 175
zalun Avatar answered Sep 23 '22 09:09

zalun


Make a note that pip version 20.2.3 from Python 3.6 requires PIP_REQUIRE_VIRTUALENV=false rather than PIP_REQUIRE_VIRTUALENV="" to install global dependencies.

like image 38
Kevin Avatar answered Sep 21 '22 09:09

Kevin