pip installing in global site-packages instead of

2019-01-04 22:26发布

Using pip to install a package in a virtualenv causes the package to be installed in the global site-packages folder instead of the one in the virtualenv folder. Here's how I set up Python3 and virtualenv on OS X Mavericks (10.9.1):

I installed python3 using Homebrew:

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
brew install python3 --with-brewed-openssl

Changed the $PATH variable in .bash_profile; added the following line:

export PATH=/usr/local/bin:$PATH

Running which python3 returns /usr/local/bin/python3 (after restarting the shell).

Note: which python3 still returns /usr/bin/python though.

Installed virtualenv using pip3:

pip3 install virtualenv

Next, create a new virtualenv and activate it:

virtualenv testpy3 -p python3
cd testpy3
source bin/activate

Note: if I don't specify -p python3, pip will be missing from the bin folder in the virtualenv.

Running which pip and which pip3 both return the virtualenv folder:

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

Now, when I try to install e.g. Markdown using pip in the activated virtualenv, pip will install in the global site-packages folder instead of the site-packages folder of the virtualenv.

pip install markdown

Running pip list returns:

Markdown (2.3.1)
pip (1.4.1)
setuptools (2.0.1)
virtualenv (1.11)

Contents of /Users/kristof/VirtualEnvs/testpy3/lib/python3.3/site-packages:

__pycache__/
_markerlib/
easy_install.py
pip/
pip-1.5.dist-info/
pkg_resources.py
setuptools/
setuptools-2.0.2.dist-info/

Contents of /usr/local/lib/python3.3/site-packages:

Markdown-2.3.1-py3.3.egg-info/
__pycache__/
easy-install.pth
markdown/
pip-1.4.1-py3.3.egg/
setuptools-2.0.1-py3.3.egg
setuptools.pth
virtualenv-1.11-py3.3.egg-info/
virtualenv.py
virtualenv_support/

As you can see, the global site-packages folder contains Markdown, the virtualenv folder doesn't.

Note: I had Python2 and Python3 installed before on a different VM (followed these instructions) and had the same issue with Python3; installing packages in a Python2 based virtualenv worked flawlessly though.

Any tips, hints, … would be very much appreciated.

21条回答
Melony?
2楼-- · 2019-01-04 22:58

Somehow a setup.cfg file with a prefix="" in the project folder

running pip install on the virtualenv outside the project folder worked so from the inside it was telling pip to use an empty prefix which defaults to "/"

removing the file fixed it

查看更多
Melony?
3楼-- · 2019-01-04 22:59

The first thing to check is which location pip is resolving to:

which pip

if you are in a virtualenv you would expect this to give you something like:

/path/to/virtualenv/.name_of_virtualenv/bin/pip

However it may be the case that it's resolving to your system pip for some reason. For example you may see this from within your virtualenv (this is bad):

/usr/local/bin/pip (or anything that isn't in your virtualenv path).

To solve this check your pipconfig in:

~/.pipconf
~/.conf/pip
/etc/pip.conf

and make sure that there is nothing that is coercing your Python path or your pip path (this fixed it for me).

Then try starting a new terminal and rebuild your virtualenv (delete then create it again)

查看更多
别忘想泡老子
4楼-- · 2019-01-04 22:59

The same problem. Python3.5 and pip 8.0.2 installed from Linux rpm's.

I did not find a primary cause and cannot give a proper answer. It looks like there are multiple possible causes.

However, I hope I can help with sharing my observation and a workaround.

  1. pyvenv with --system-site-packages

    • ./bin does not contain pip, pip is available from system site packages
    • packages are installed globally (BUG?)
  2. pyvenv without --system-site-packages

    • pip gets installed into ./bin, but it's a different version (from ensurepip)
    • packages are installed within the virtual environment (OK)

Obvious workaround for pyvenv with --system-site-packages:

  • create it without the --system-site-packages option
  • change include-system-site-packages = false to true in pyvenv.cfg file
查看更多
登录 后发表回答