1

I have Django 1.10 installed within a virtualenv on my machine. Now I am creating another virtualenv (for another project) and installing Django 1.11 on it using the following command:

pip install Django

but I get a permission denied error:

Collecting Django
  Using cached Django-1.11.5-py2.py3-none-any.whl
Requirement already satisfied: pytz in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (from Django)
Installing collected packages: Django
Exception:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pip/commands/install.py", line 342, in run
    prefix=options.prefix_path,
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pip/req/req_set.py", line 784, in install
    **kwargs
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pip/req/req_install.py", line 851, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pip/req/req_install.py", line 1064, in move_wheel_files
    isolated=self.isolated,
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pip/wheel.py", line 377, in move_wheel_files
    clobber(source, dest, False, fixer=fixer, filter=filter)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pip/wheel.py", line 323, in clobber
    shutil.copyfile(srcfile, destfile)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/shutil.py", line 115, in copyfile
    with open(dst, 'wb') as fdst:
PermissionError: [Errno 13] Permission denied: '/Library/Frameworks/Python.framework/Versions/3.5/bin/__pycache__/django-admin.cpython-35.pyc'

I have read multiple SO posts about this but their solutions dont seem to apply to me. Please note:

1) I have already activated the new virtualenv before running the command.

2) I did not create the new virtualenv using sudo. I just did the following to create it:

virtualenv name-of-the-new-virtualenv

What could I be missing?

Anupam
  • 14,950
  • 19
  • 67
  • 94
  • 1
    What are the results of running `which pip` and `which python` after sourcing the virtualenv you're trying to install Django into? Also, what does your VIRTUAL_ENV and PATH look like (`echo "$VIRTUAL_ENV"` and `echo "$PATH"`)? – Brandon DeRosier Sep 13 '17 at 07:00
  • 1
    Brandon, you solved the problem. `which pip` and `which python` returned system paths and not the virtualenv paths. Then `echo "$VIRTUAL_ENV"` returned a path that did not exist! I had actually moved the virtualenv directory to home directory from project directory to keep all virtualenv directories at the same place (I think I read somewhere that we can move them around but looks like that was not a good idea). I moved it back and it worked fine. Feel free to write an answer so I can give you credit – Anupam Sep 13 '17 at 07:13
  • Great! Glad it helped. I answered with some additional general information about virtualenvs to help future onlookers. – Brandon DeRosier Sep 13 '17 at 07:30

1 Answers1

1

When using bash, the version of python being resolved in the PATH can be seen at any time by using which python.

You can also check the location of your sourced virtualenv by viewing the VIRTUAL_ENV environment variable (e.g. echo $VIRTUAL_ENV).

In this case, the issue was that the virtualenv directory had been moved after being created, so the PATH environment variable wasn't getting populated with the virtualenv's correct bin directory upon sourcing. One solution for moving a virtualenv is to make it "relocatable", which is explained in this answer.

Virtualenvs can be activated by sourcing the activate script:

source /path/to/my/venv/bin/activate

When any virtualenv has been sourced, it can be deactivated by using the deactivate function:

deactivate
Brandon DeRosier
  • 861
  • 5
  • 15