为什么我没有权限在 virtualenv 中安装 psycopg2

Why don't I have permission to install psycopg2 in a virtualenv

我的目标是在 EC2 实例 运行 Ubuntu 上提供一个使用 postgres 的 hello world Django 应用程序。我通过 ssh 登录并克隆了一个 git 存储库,其中包含一个 Django 项目 requirements.txt:

Django==1.8.2  
djangorestframework==3.1.2  
psycopg2==2.6

我创建了一个 virtualenv 然后,当我 运行 (ec2_deploy_test)ubuntu@ip-172-31-22-100:~/ec2-deploy-test$ pip install -r requirements.txt 时,抛出了这个异常:

Collecting psycopg2==2.6 (from -r requirements.txt (line 3)) /home/ubuntu/Envs/ec2_deploy_test/local/lib/python2.7/site-packages/pip/vendor/requests/packages/urllib3/util/ssl.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning. InsecurePlatformWarning Downloading psycopg2-2.6.tar.gz (367kB) 100% |████████████████████████████████| 368kB 785kB/s Building wheels for collected packages: psycopg2 Exception: Traceback (most recent call last): File "/home/ubuntu/Envs/ec2_deploy_test/local/lib/python2.7/site-packages/pip/basecommand.py", line 223, in main status = self.run(options, args) File "/home/ubuntu/Envs/ec2_deploy_test/local/lib/python2.7/site-packages/pip/commands/install.py", line 291, in run wb.build(autobuilding=True) File "/home/ubuntu/Envs/ec2_deploy_test/local/lib/python2.7/site-packages/pip/wheel.py", line 753, in build ensure_dir(output_dir) File "/home/ubuntu/Envs/ec2_deploy_test/local/lib/python2.7/site-packages/pip/utils/init.py", line 70, in ensure_dir os.makedirs(path) File "/home/ubuntu/Envs/ec2_deploy_test/lib/python2.7/os.py", line 150, in makedirs makedirs(head, mode) File "/home/ubuntu/Envs/ec2_deploy_test/lib/python2.7/os.py", line 150, in makedirs makedirs(head, mode) File "/home/ubuntu/Envs/ec2_deploy_test/lib/python2.7/os.py", line 150, in makedirs makedirs(head, mode) File "/home/ubuntu/Envs/ec2_deploy_test/lib/python2.7/os.py", line 157, in makedirs mkdir(name, mode) OSError: [Errno 13] Permission denied: '/home/ubuntu/.cache/pip/wheels/ab'

然后我运行(ec2_deploy_test)ubuntu@ip-172-31-22-100:~/ec2-deploy-test$ sudo pip install -r requirements.txt和psycopg2安装成功

为什么我需要 root 权限才能在我的虚拟环境中安装 python 软件包?我是 Linux 和系统管理员的新手,所以欢迎所有建议。提前致谢。

出于某种原因,您无权在 /home/ubuntu/.cache/pip/wheels/ab 中创建目录。正常应该不会出现这个问题;反正既然发生了,就递归改一下.cache目录的权限就好了。我想问题出在所有权上,因此请尝试启动命令 sudo chown -R <USERNAME> ~/.cache/pip,其中 <USERNAME> 应该是您的用户名。

一个建议 — 尽量不要在没有真正需要的情况下从 root 启动应用程序。您无权访问的目录很可能是由某些 运行 具有 root 权限的应用程序创建的 — 现在所有权一团糟。

我假设你使用了 virtualenv -p /usr/bin/python3.4 env, 我有同样的问题

它是 python 3,如果您尝试了 Angles 推荐但它不起作用,则可能与权限或 root 无关。您需要安装 python3-dev ... 或者坚持使用 python2。希望有帮助

这里是 link:Trouble with psycopg2 in virtualenv python3 for use with Django