Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System PIP instead of virtualenv PIP by default?

After using virtualenv with pip off-and-on for a couple of days, I've found that the version of PIP that is used after the virtualenv is actived is the global PIP instead of the PIP relative to that environment; such that if you don't set the shell environment variable export PIP_RESPECT_VIRTUALENV=true, pip will install whatever new package (e.g. pip install argparse) to the global scope instead of only to the virtualenv.

I would expect PIP to install to the virtualenv by default, if that virtualenv is activated.

Is there a reasoning behind it not working that way by default?

See explanation here for how PIP_RESPECT_VIRTUALENV works.

like image 958
bitcycle Avatar asked Feb 10 '12 21:02

bitcycle


People also ask

How do I turn off pip VENV?

You can deactivate a virtual environment by typing “deactivate” in your shell. The exact mechanism is platform-specific and is an internal implementation detail (typically a script or shell function will be used).

Does VENV come with pip?

In fact, virtualenv comes with a copy of pip which gets copied into every new environment you create, so virtualenv is really all you need. You can even install it as a separate standalone package (rather than from PyPI).

Do I need to install pip in VENV?

With the default settings, venv will install both pip and setuptools. Using pip is the recommended way to install packages in Python, and setuptools is a dependency for pip . Because installing other packages is the most common use case for Python virtual environments, you'll want to have access to pip .


2 Answers

It is not the first time I see someone reporting the same issue. I don't know what is happening, but some people discourage the use o source /path/to/venv/bin/activate because it can mess up your $PATH.

There is a way pip will always respect your virtualenv: don't rely on $PATH. Use:

/path/to/venv/bin/pip install MYPACKAGE

It would be nice to find out what is happening to you and share your solution with others. Meanwhile, it may be ok to use the absolute path to pip.

like image 194
Hugo Tavares Avatar answered Nov 15 '22 03:11

Hugo Tavares


When you create a virtualenv, the activate file hardcodes the variable VIRTUAL_ENV to the location in which you first created the root directory. This variable is then exported when you source <your-venv>/bin/activate.

Consequently, if you move the virtualenv directory subsequent to its creation, the hardcoded file path will be incorrect.

Just open <your-venv>/bin/activate in a text editor and make sure VIRTUAL_ENV is set to the new path of your virtualenv directory:

VIRTUAL_ENV="/Full/path/to/<your-venv>"
export VIRTUAL_ENV

before running source <your-venv>/bin/activate again.

Then of course you can test the version of pip with which pip which should produce:

/Full/path/to/<your-venv>/bin/pip

rather than /usr/bin/pip or /bin/pip etc.

like image 28
toxefa Avatar answered Nov 15 '22 03:11

toxefa