python, virtualenv, pip and sudo/permissions

The problem:

  • To install a virtualenv (virtual environment for python) under a common (not user) folder, so that it is accessible to other users.

If we try without using sudo, we get a permission error:

[go into virtualenv called env]
pip3 install fibo
>[lots of stuff]
>PermissionError: [Errno 13] Permission denied: '/django/env/lib/python3.4/site-packages/Fibo.py'

If we try with sudo, it installs it in the wrong location:

sudo which pip3
>/usr/bin/pip3

I.e. it is installing it in the system-wide location. This isn’t accessible from within the virtualenv. What to do? Some links I found:

Of these, the first does not address the problem of running pip, which isn’t a python script. The second gives a command that doesn’t work (-E option does not work for me). The third addresses an issue that is almost right, but the solution appears not to be related.

After some time, it occurred to me to call the right pip3 with sudo.

sudo -H env/bin/pip install django
[install stuff]
sudo -H env/bin/pip install list
>Django (1.9.1)
>pip (8.0.0)
>setuptools (19.4)
>wheel (0.26.0)

Success!

Better solution

Just change the permissions so that all users can edit the files. Then one does not need sudo at all.

sudo chmod -R o+rwx /directory