如何在 Python virtualenv 中安装 gevent?

How to install gevent in a Python virtualenv?

我正在尝试在我的 Ubuntu 14.04 服务器上使用 virtualenv,但我似乎无法安装 gevent。我首先激活 virtualenv,然后我使用 pip:

安装 gevent
$ source venv/bin/activate
(venv)immoh@vgmt:~/immod$ sudo pip install gevent
The directory '/home/immoh/.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.
The directory '/home/immoh/.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.
Requirement already satisfied (use --upgrade to upgrade): gevent in /usr/local/lib/python2.7/dist-packages
Requirement already satisfied (use --upgrade to upgrade): greenlet>=0.4.7 in /usr/local/lib/python2.7/dist-packages (from gevent)

在此之后,我尝试从 python 命令行使用它:

(venv)immoh@vgmt:~/immod$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gevent
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named gevent
>>>

这似乎没有用,所以我尝试按照上面消息的建议使用 sudo's -H flag 安装它,但在那之后,我又无法导入 gevent:

(venv)immoh@vgmt:~/immod$ sudo -H pip install gevent
Requirement already satisfied (use --upgrade to upgrade): gevent in /usr/local/lib/python2.7/dist-packages
Requirement already satisfied (use --upgrade to upgrade): greenlet>=0.4.7 in /usr/local/lib/python2.7/dist-packages (from gevent)
(venv)immoh@vgmt:~/immoh$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gevent
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named gevent

有谁知道我在这里做错了什么以及我该如何解决这个问题?欢迎所有提示!

问题的根源是您在不应该使用的时候使用了 sudo。一旦你在 virtualenv 中工作,只有 运行 pip 而没有 sudo 你应该有 none 这些问题。

顺便说一下,我建议创建一个新的 virtualenv 来尝试这个,以避免您之前的尝试造成的任何问题。