Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pip installing in global site-packages instead of virtualenv

People also ask

Does pip install global packages?

The Pip Package Manager can be used to list both globally and locally installed Python packages.

How do I install pip in a specific environment?

For the environments use https://conda.io/docs/user-guide/tasks/manage-environments.html to activate the desired environment, then you can pip install or conda install the packages and it will be places safely only in that environment.

Do I need to install pip in VENV?

Usually, you install pip OUTSIDE of your virtual environment. But after activating the virtualenv you just run "pip install" inside the environment. Meaning, you install it outside the virtualenv.

Can Python virtual environment use global packages?

Python virtual environments allow you to install Python packages in an isolated location for a particular application instead of installing them globally.


Funny you brought this up, I just had the exact same problem. I solved it eventually, but I'm still unsure as to what caused it.

Try checking your bin/pip and bin/activate scripts. In bin/pip, look at the shebang. Is it correct? If not, correct it. Then on line ~42 in your bin/activate, check to see if your virtualenv path is right. It'll look something like this

VIRTUAL_ENV="/Users/me/path/to/virtual/environment"

If it's wrong, correct it, deactivate, then . bin/activate, and if our mutual problem had the same cause, it should work. If it still doesn't, you're on the right track, anyway. I went through the same problem solving routine as you did, which piping over and over, following the stack trace, etc.

Make absolutely sure that

/Users/kristof/VirtualEnvs/testpy3/bin/pip3

is what you want, and not referring to another similarly-named test project (I had that problem, and have no idea how it started. My suspicion is running multiple virtualenvs at the same time).

If none of this works, a temporary solution may be to, as Joe Holloway said,

Just run the virtualenv's pip with its full path (i.e. don't rely on searching the executable path) and you don't even need to activate the environment. It will do the right thing.

Perhaps not ideal, but it ought to work in a pinch.

Link to my original question:

VirtualEnv/Pip trying to install packages globally


For me this was not a pip or virtualenv problem. It was a python problem. I had set my $PYTHONPATH manually in ~/.bash_profile (or ~/.bashrc) after following some tutorial online. This manually set $PYTHONPATH was available in the virtualenv as it probably should be allowed.

Additionally add2virtualenv was not adding my project path to my $PYTHONPATH for some reason within the virtualenv.

Just some forking paths for those who might still be stuck! Cheers!


I had the same problem, I solved it by removing venv directory and recreating it!

deactivate (if venv is activated first deactivate it)
rm -rf venv
virtualenv -p python3 venv
. ENV/bin/activate
pip3 install -r requirements.txt

Now everything works like a charm.


I had the same issue on macos with python 2 and 3 installed.

Also, I had aliases to point to python3 and pip3 in my .bash_profile.

alias python=/usr/local/bin/python3
alias pip=/usr/local/bin/pip3

Removing aliases and recreating virtual env using python3 -m venv venv fixed the issue.