Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is pip, inside a virtualenv, writing to /usr/lib?

I'm running Python on a slightly older OS, whose system Python is at version 2.6. Hence I have 2.7 installed in my home directory and used that Python to get pip, and used that pip to install virtualenvwrapper. So I have

$ which python pip virtualenv virtualenvwrapper.sh
/home/user/bin/python
/home/user/bin/pip
/home/user/.local/bin/virtualenv
/home/user/.local/bin/virtualenvwrapper.sh

I use these tools to create a virtualenv, and try to install a module

$ mkvirtualenv fred
New python executable in fred/bin/python2.7.10
Also creating executable in fred/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /home/user/.virtualenvs/fred/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/user/.virtualenvs/fred/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/user/.virtualenvs/fred/bin/preactivate
virtualenvwrapper.user_scripts creating /home/user/.virtualenvs/fred/bin/postactivate
virtualenvwrapper.user_scripts creating /home/user/.virtualenvs/fred/bin/get_env_details

$ workon fred
$ export PYTHONPATH=/home/user/.virtualenvs/fred/lib/python2.7/site-packages
$ /home/user/.virtualenvs/fred/bin/easy_install --prefix=/home/user/.virtualenvs/fred pip
Creating /home/user/.virtualenvs/fred/lib/python2.7/site-packages/site.py
Searching for pip
Best match: pip 7.1.2
Adding pip 7.1.2 to easy-install.pth file
Installing pip script to /home/user/.virtualenvs/fred/bin
Installing pip3.4 script to /home/user/.virtualenvs/fred/bin
Installing pip3 script to /home/user/.virtualenvs/fred/bin

Using /home/user/.virtualenvs/fred/lib/python2.7/site-packages                                                                                                           
Processing dependencies for pip
Finished processing dependencies for pip

$ which python pip 
/home/user/.virtualenvs/fred/bin/python
/home/user/.virtualenvs/fred/bin/pip

$ pip install itsdangerous
Collecting itsdangerous
Installing collected packages: itsdangerous

Exception:
Traceback (most recent call last):
  File "/home/user/.local/lib/python2.7/site-packages/pip/basecommand.py", line 211, in main
    status = self.run(options, args)
  File "/home/user/.local/lib/python2.7/site-packages/pip/commands/install.py", line 311, in run
    root=options.root_path,
  File "/home/user/.local/lib/python2.7/site-packages/pip/req/req_set.py", line 646, in install
    **kwargs
  File "/home/user/.local/lib/python2.7/site-packages/pip/req/req_install.py", line 803, in install
    self.move_wheel_files(self.source_dir, root=root)
  File "/home/user/.local/lib/python2.7/site-packages/pip/req/req_install.py", line 998, in move_wheel_files
    isolated=self.isolated,
  File "/home/user/.local/lib/python2.7/site-packages/pip/wheel.py", line 341, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/home/user/.local/lib/python2.7/site-packages/pip/wheel.py", line 319, in clobber
    shutil.copyfile(srcfile, destfile)
  File "/usr/lib64/python2.7/shutil.py", line 83, in copyfile
    with open(dst, 'wb') as fdst:
IOError: [Errno 13] Permission denied: '/usr/lib/python2.7/site-packages/itsdangerous.py'

Why is pip trying to install the module into /usr/lib? Shouldn't it be installed to ~/.virtualenvs/fred/lib, or ~/lib?

Status as of Jul 4th, 2018: Unreproducible, as I do not have access to that company's servers any more, and I have not seen it on other systems since.

like image 372
jalanb Avatar asked Oct 14 '15 15:10

jalanb


People also ask

Does virtualenv install pip?

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 .


1 Answers

Probably your are calling the system level pip and not the virtualenv pip.

You should activate your virtualenv before:

. bin/activate
like image 105
Fabio Caccamo Avatar answered Oct 20 '22 01:10

Fabio Caccamo