无法在EC2上的virtualenv中pip安装软件包 [英] Cannot pip install package in virtualenv on EC2
问题描述
我在ec2上看到了这个奇怪的问题.我正在尝试在我的virtualenv中安装lsm-db
软件包,它说已成功安装,但是在尝试导入该软件包或执行pip list
时不存在.
I'm seeing this weird issue on ec2. I'm trying to install lsm-db
package inside my virtualenv, it says it's successfully installed but when trying to import the package or do pip list
it's not there.
我创建了这个virtualenv,
I created this virtualenv by
virtualenv -p python3.6 venv
source venv/bin/activate
然后运行which python
和which pip
会将venv
内部的正确显示为<path-to-my-project>/venv/bin/pip
.然后运行pip install -r requirements.txt
,其中包括sqlalchemy
和lsm-db
.显示警告
then running which python
and which pip
shows the correct ones inside venv
as <path-to-my-project>/venv/bin/pip
. Then I run pip install -r requirements.txt
where sqlalchemy
and lsm-db
are included. A warning is shown
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给出
The last line shows they are correctly installed despite the warnings. However, they are not. Running pip install lsm-db gives
▶ 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
的方式有关吗?
Again it shows it's installed, but it's not. Not in pip list, can't import in Python. What is going on here? Why do I see permission issue inside virtualenv
? I didn't create the virtualenv
with sudo if that's a potential problem. Is it related to the way I installed virtualenv
?
编辑
我更改了权限,并使用sudo yum install python36-pip
重新安装了pip.现在我看到了...抱歉,这似乎是一个新问题
I changed the permission and reinstall pip with sudo yum install python36-pip
. Now I'm seeing this... sorry it seems like a new problem
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/
编辑
EDIT
感谢指针,现在我在ec2上安装了python开发包,并尝试再次安装lsm-db
Thanks for the pointers, now I installed the python development package on ec2 and tried to install lsm-db again
> 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中导入它.
again, it shows it's successful, and which python
gives me the right python in my venv. However, I have the exact same result as the beginning, pip list
does not have lsm-db
and I cannot import it in python.
这完全是ec2上的问题,我可以在Mac上的virtualenv中成功使用lsm-db
.此处ec2上的virtualenv
必须关闭,为什么venv中正确的点不能在venv中安装软件包?我完全迷路了.如果有人有经验,请报名!
This is entirely an issue on ec2, I can successfully use lsm-db
in my virtualenv on my mac. There must be something off with virtualenv
on ec2 here, why the right pip in venv cannot install a package in venv?? I'm completely lost. If anyone has experience please chime in!
(要变通解决此问题,我将代码中的lsm-db
切换为sqlitedict
.可以毫无问题地安装sqlitedict
,因此将可疑范围缩小到了lsm-db
特定问题.)
(To work around this problem, I'm switching from lsm-db
to sqlitedict
in my code. sqlitedict
can be installed no problem, so it narrows the suspect down to just lsm-db
specific issues.)
2016年的这个问题与我的问题更相似,但没有结论为什么或任何解决方案. Pip说pymongo已经被已安装,但未显示为已安装
This question from 2016 is more similar to mine but it has no conclusion why or any solution. Pip says that pymongo has been installed, but doesn't show it as installed
另一个可能相关的问题: Amazon EC2 virtualenv:pip说它安装了numpy但python找不到它
Another potentially related question: Amazon EC2 virtualenv: pip says it installed numpy but python can't find it
推荐答案
问题似乎出在系统文件权限上.您可以使用以下方法从终端检查所有者':group以及文件夹'/home/ec2-user/.cache/pip'的其他用户的 rwx 权限:
The problem seems to be with system file permissions. You can check the owner:group as well as rwx permissions for other users of the folder '/home/ec2-user/.cache/pip' from terminal using:
ls -lh/home/ec2-user/.cache/pip
ls -lh /home/ec2-user/.cache/pip
要更改文件夹的所有者,请使用命令:
In order to change the owner of the folder use command:
sudo chown -R ec2-user/home/ec2-user/.cache/pip
sudo chown -R ec2-user /home/ec2-user/.cache/pip
另一种解决方法是通过以下方式允许所有用户使用rwx权限:
Another way to solve is to allow rwx permissions to all users by:
sudo chmod -R 777/home/ec2-user/.cache/pip
sudo chmod -R 777 /home/ec2-user/.cache/pip
这篇关于无法在EC2上的virtualenv中pip安装软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!