【问题标题】:Cannot pip install package in virtualenv on EC2无法在 EC2 上的 virtualenv 中 pip 安装包
【发布时间】:2019-11-28 00:31:06
【问题描述】:

我在 ec2 上看到了这个奇怪的问题。我正在尝试在我的 virtualenv 中安装 lsm-db 包,它说它已成功安装,但是在尝试导入包或执行 pip list 时它不存在。

我创建了这个虚拟环境

virtualenv -p python3.6 venv
source venv/bin/activate

然后运行which pythonwhich pip 会在venv 中显示正确的<path-to-my-project>/venv/bin/pip。然后我运行pip install -r requirements.txt,其中包括sqlalchemylsm-db。显示警告

WARNING: Building wheel for lsm-db failed: [Errno 13] Permission denied: '/home/ec2-user/.cache/pip'
  WARNING: Building wheel for sqlalchemy failed: [Errno 13] Permission denied: '/home/ec2-user/.cache/pip'
Failed to build lsm-db sqlalchemy
Installing collected packages: sqlalchemy, flask-sqlalchemy, lsm-db, MarkupSafe, click
  Running setup.py install for sqlalchemy ... done
  Running setup.py install for lsm-db ... done
Successfully installed MarkupSafe-1.1.1 click-7.0 flask-sqlalchemy-2.4.0 lsm-db-0.6.4 sqlalchemy-1.3.5

尽管有警告,最后一行显示它们已正确安装。但是,它们不是。运行 pip install lsm-db 给出了

▶ pip install lsm-db
WARNING: The directory '/home/ec2-user/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
WARNING: The directory '/home/ec2-user/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting lsm-db
  Downloading https://files.pythonhosted.org/packages/17/f2/dc68ecdece424eb206c42b8db924c5a7f6dd97d26df42a1c9fb41a6fe555/lsm-db-0.6.4.tar.gz (316kB)
     |████████████████████████████████| 317kB 14.7MB/s
Building wheels for collected packages: lsm-db
  WARNING: Building wheel for lsm-db failed: [Errno 13] Permission denied: '/home/ec2-user/.cache/pip'
Failed to build lsm-db
Installing collected packages: lsm-db
  Running setup.py install for lsm-db ... done
Successfully installed lsm-db-0.6.4

再次显示它已安装,但事实并非如此。不在 pip 列表中,无法在 Python 中导入。这里发生了什么?为什么我在virtualenv 中看到权限问题?如果这是一个潜在问题,我没有使用 sudo 创建virtualenv。是不是和我安装virtualenv的方式有关?


编辑

我更改了权限并使用sudo yum install python36-pip 重新安装了 pip。现在我看到了……抱歉,这似乎是一个新问题

Running setup.py install for lsm-db ... error
    ERROR: Complete output from command /home/ec2-user/<project>/venv/bin/python3.6 -u -c 'import setuptools, tokenize;__file__='"'"'/tmp/pip-install-bhhmd3j8/lsm-db/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-__gotpo6/install-record.txt --single-version-externally-managed --compile --install-headers /home/ec2-user/<project>/venv/include/site/python3.6/lsm-db:
    ERROR: /tmp/pip-install-bhhmd3j8/lsm-db/setup.py:11: UserWarning: Cython not installed, using pre-generated C source file.
      warnings.warn('Cython not installed, using pre-generated C source file.')
    running install
    running build
    running build_ext
    building 'lsm' extension
    creating build
    creating build/temp.linux-x86_64-3.6
    creating build/temp.linux-x86_64-3.6/src
    gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/include/python3.6m -c lsm.c -o build/temp.linux-x86_64-3.6/lsm.o
    lsm.c:32:20: fatal error: Python.h: No such file or directory
     #include "Python.h"
                        ^
    compilation terminated.
    error: command 'gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command "/home/ec2-user/<project>/venv/bin/python3.6 -u -c 'import setuptools, tokenize;__file__='"'"'/tmp/pip-install-bhhmd3j8/lsm-db/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-__gotpo6/install-record.txt --single-version-externally-managed --compile --install-headers /home/ec2-user/<project>/venv/include/site/python3.6/lsm-db" failed with error code 1 in /tmp/pip-install-bhhmd3j8/lsm-db/

编辑

感谢指点,现在我在ec2上安装了python开发包,尝试再次安装lsm-db

> sudo yum install python36-devel
...
> pip install lsm-db
Collecting lsm-db
  Using cached https://files.pythonhosted.org/packages/17/f2/dc68ecdece424eb206c42b8db924c5a7f6dd97d26df42a1c9fb41a6fe555/lsm-db-0.6.4.tar.gz
Building wheels for collected packages: lsm-db
  Building wheel for lsm-db (setup.py) ... done
  Stored in directory: /home/ec2-user/.cache/pip/wheels/ed/32/8a/b0f3aa15b68f1e6b8f2dcc6285de4796b6b58af71c5f280f48
Successfully built lsm-db
Installing collected packages: lsm-db
Successfully installed lsm-db-0.6.4

再次,它表明它是成功的,which python 在我的 venv 中为我提供了正确的 python。但是,我得到的结果和开始完全一样,pip list 没有lsm-db,我无法在 python 中导入。

这完全是 ec2 的问题,我可以在我的 mac 上的 virtualenv 中成功使用lsm-db。 ec2 上的virtualenv 肯定有问题,为什么 venv 中的正确 pip 无法在 venv 中安装包?我完全迷路了。有经验的请指教!

(为了解决这个问题,我在我的代码中从lsm-db 切换到sqlitedictsqlitedict 可以安装没有问题,因此它将嫌疑人缩小到lsm-db 特定问题。)

2016 年的这个问题与我的更相似,但没有结论为什么或任何解决方案。 Pip says that pymongo has been installed, but doesn't show it as installed

另一个可能相关的问题: Amazon EC2 virtualenv: pip says it installed numpy but python can't find it

【问题讨论】:

  • 更改.cache的权限怎么样?
  • 是的,或者直接删除.cache目录。
  • 我相信如果您之前使用sudo 运行一些命令,这些命令也使用此.cache 目录,则可能会发生此问题。因此它将为 root 用户创建。虽然大部分.cache目录会在/root下创建,但有时有些软件就是不能正确处理。
  • 感谢所有 cmets,有一个新问题...
  • 需要安装Python开发包才能获取所需的头文件。

标签: python amazon-ec2 pip


【解决方案1】:

问题似乎与系统文件权限有关。您可以使用以下命令从终端检查文件夹“/home/ec2-user/.cache/pip”的其他用户的所有者:组以及 rwx 权限:

ls -lh /home/ec2-user/.cache/pip

为了更改文件夹的所有者使用命令:

sudo chown -R ec2-user /home/ec2-user/.cache/pip

另一种解决方法是通过以下方式向所有用户授予 rwx 权限:

sudo chmod -R 777 /home/ec2-user/.cache/pip

【讨论】:

  • 感谢您的回答,请您看一下编辑,经过一些修补后我有一个新问题..
猜你喜欢
  • 2020-03-18
  • 2017-07-27
  • 2015-07-25
  • 1970-01-01
  • 1970-01-01
  • 2015-09-28
  • 1970-01-01
  • 2016-01-24
  • 1970-01-01
相关资源
最近更新 更多