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

11
  • Does it work correctly for other packages? Do you have the same problem if you use easy_install?
    – larsks
    Jul 26, 2013 at 17:28
  • Same problem with other packages. And I believe easy_install requires sudo and would install into the global directory.
    – Leah Sapan
    Jul 26, 2013 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, 2013 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, 2013 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.
    – Leah Sapan
    Jul 26, 2013 at 18:20

8 Answers 8

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.

4
  • Yeah that was exactly it. In my case I was able to just re-create the virtualenv from scratch and that solved it.
    – Leah Sapan
    Jun 6, 2015 at 16:29
  • Glad to be of help
    – Crispy
    Jun 6, 2015 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
    – ki9
    Feb 25, 2017 at 6: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, 2020 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...

0
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.

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.

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.

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/' *

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)

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.