Tuesday, February 1, 2022

[SOLVED] Permission error when installing Django within virtual environment

Issue

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?


Solution

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


Answered By - Brandon DeRosier
Answer Checked By - Marilyn (WPSolving Volunteer)