13

I am attempting to install django to a virtualenv that already exists.

Following the instructions listed on the pip-install website here, I ran the following from SSH.

name@server:~$ . myenv.env/bin/activate
(myenv.env)nam@server:~$ pip install django

However at the bottom of the installation, I am seeing this:

creating /usr/local/lib/python2.7/dist-packages/django

error: could not create '/usr/local/lib/python2.7/dist-packages/django': Permission denied

It appears that it is trying to install it to the global directory. I do not have sudo privileges. Am I doing something wrong here?

Update: $PATH = /var/django/myenv.env/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

Luke Sapan
  • 189
  • 1
  • 2
  • 15
  • Does it work correctly for other packages? Do you have the same problem if you use `easy_install`? – larsks Jul 26 '13 at 17:28
  • Same problem with other packages. And I believe easy_install requires sudo and would install into the global directory. – Luke Sapan Jul 26 '13 at 17:53
  • Nope, setting up a virtualenv gets you easy_install as well as pip. You can type `which easy_install` to see if you're using your virtualenv or the system one. – larsks Jul 26 '13 at 17:55
  • Actually, are you sure you're running pip from inside the virtualenv? What does `which pip` yield? And do you see `pip` inside `myenv.env/bin/`? – larsks Jul 26 '13 at 17:56
  • I do see pip inside of myenv.env/bin/. I just tried "activating" my env again, and despite being in that mode, "which pip" and "which easy_install" both return "usr/local/bin/pip" and "usr/bin/easy_install" respectively. – Luke Sapan Jul 26 '13 at 18:20
  • What does your `$PATH` look like (please update the question rather than posting that as a comment)? Also, what if you explicitly run `myenv.env/bin/pip install ...`? – larsks Jul 26 '13 at 18:27
  • I updated the question with the path. And I was originally trying that but it says "bad interpreter: No such file or directory". The other strange thing is even though I'm typing myenv.env/bin/pip, it is saying myenv.env/bin/python: bad interpreter – Luke Sapan Jul 26 '13 at 18:42
  • Ist there a `myenv.env/bin/python` at all? – etagenklo Jul 26 '13 at 19:01
  • Yes there is, and if I try to execute the python in that folder it successfully starts interactive python. – Luke Sapan Jul 26 '13 at 19:17
  • Could you try to create a virtualenv with another name, such like "ve1" and try it from there? – erny Nov 29 '13 at 18:02
  • I was able to install requirements explicitly using pip from venv/bin/pip3 – Carmine May 25 '18 at 09:19

8 Answers8

8

Sorry for a year late answer! I had the same problem and fixed it, I don't know if you changed the name of a directory after creating the virtual environment, I did though. If so then here's what I did.

1.) deactivate your v-env. After the fix you need to restart the v-env, so might as well deactivate now. right?

2.) Now, since we created the v-env in a different path, we have to change the static path variables in these files.

To get pip working you don't need to do this, but I still do. bin/activate, bin/activate.csh, bin/activate.fish

bin/pip, bin/pip2, bin/pip2.7

bin/easy_install, bin/easy_install2.7

3.) To get pip working, you must correct the python interpreter in the pip file, this as well has a static interpreter location set by virtualenv in the creation process.

4.) To get easy_install working? You guessed it, fix the interpreter location.

I hope this helped for any people reading this in the future. Sorry OP, for being late.

Crispy
  • 196
  • 1
  • 1
  • Yeah that was exactly it. In my case I was able to just re-create the virtualenv from scratch and that solved it. – Luke Sapan Jun 06 '15 at 16:29
  • Glad to be of help – Crispy Jun 06 '15 at 16:41
  • I'm so glad to finally find an answer. For me, using sed in the venv folder made the job a lot easier. Something like `grep -rli '/path/to/old/env/bin' * | xargs -i@ sed -i 's/\/path\/to\/old\/env\/bin/\/path\/to\/new\/env\/bin/g' @`.[source](http://stackoverflow.com/a/20721292/3006854) – ki9 Feb 25 '17 at 06:56
  • 1
    You say, "To get pip working, you must correct the python interpreter in the pip file", where and how is this done? – Dave Apr 20 '20 at 16:31
3

I had this same problem.

I deleted the virtual environment and created a new one, which solved the problem.

Probably not the answer you were hoping for, but since it's the only one...

Mark
  • 221
  • 3
  • 6
1

Calling sudo pip will call global pip and not pip in your virtualenv. Activate/Workon your environment then just call pip, not sudo pip, this may fix your issue, as it did mine.

Marco
  • 1,679
  • 3
  • 17
  • 31
1

Well without administrative privileges you're very limited on what you are able to do. If you are not allowed to elevate yourself or ask for privileges, the best way I found to go about that would be to create another environment, make a requirements.txt file, download all the packages you need to your machine( django ) that would also be located in your requirements file and it should work.

secure212
  • 228
  • 1
  • 3
  • 10
0

In may case I had upgraded my main python version and this was not reflected in the virtualenv. So to upgrade pip, make sure your virtualenv is activated, then run

python -m ensurepip --upgrade
Psionman
  • 101
  • 1
0

Had the same problem. In my case the reason was that the created virtual env was for python2.7 (the default) but I was using pip3 to install a package. pip3 was not present in my virtualenv so it defaulted to the global one. For me the fix was to use

virtualenv flask --python=python3

to create the env.

Adversus
  • 121
  • 3
0

I had encountered the same problem caused by renaming of user. Crispy's answer is totally right. And my solution may be more convenient.

setps:
1. Enter your virtual environment's bin path, such as cd ~/virenv_dir/bin
2. Rename all files under this directory using sed command. sed -i 's/old_name/new_name/' *

Vaibhav Panmand
  • 959
  • 5
  • 17
xialu
  • 1
0

In my case, I had defined two aliases (to overcome some other issue on the default python version):

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

And this was causing the same symptoms:

[Errno 13] Permission denied: '/usr/lib/python3.6/site-packages'

Removing the aliases solved the issue (before or after creating the virtualenv)

coderazzi
  • 101
  • 2