在虚拟环境中安装Django时的权限错误

时间:2017-09-13 06:52:40

标签: django pip virtualenv

我在我的机器上的virtualenv中安装了Django 1.10。现在我正在创建另一个virtualenv(用于另一个项目)并使用以下命令在其上安装Django 1.11:

pip install Django

但是我得到了一个拒绝权限错误:

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'

我已阅读多篇有关此内容的SO帖子,但他们的解决方案似乎并不适用于我。请注意:

1)在运行命令之前,我已经已经激活新的virtualenv。

2)我没有使用sudo创建新的virtualenv。我只是做了以下创建它:

virtualenv name-of-the-new-virtualenv

我能错过什么?

1 个答案:

答案 0 :(得分:1)

使用bash时,可以使用PATH随时查看which python中正在解析的python版本。

您还可以查看VIRTUAL_ENV环境变量(例如echo $VIRTUAL_ENV)来检查来源virtualenv的位置。

在这种情况下,问题是virtualenv目录在创建后已被移动,因此PATH环境变量没有填充virtualenv'在采购时纠正bin目录。移动virtualenv的一个解决方案是使它可以重新定位",这在this answer中有解释。

可以通过寻找activate脚本来激活Virtualenvs:

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

当获得任何virtualenv时,可以使用deactivate函数停用它:

deactivate