Python interpteter 无法在 virtualenv 中找到模块,但是 pip 可以看到它并且不会安装

Python interpteter can't find module in virtualenv, but pip sees it and won't install

我正在尝试在 Nervana 为他们的 Neon 深度学习包创建的 virtualenv 中使用 pytools 模块,但似乎无法找到 pytools 或 pip 它。当我进入我的 virtualenv 时,我看到了这个行为:

me@ARL--M6800:~/Downloads/neon$ source .venv/bin/activate
(.venv) me@ARL--M6800:~/Downloads/neon$ 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 pytools
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  ImportError: No module named pytools
>>> 

>>> import sys
>>> sys.path
['', '/usr/local/lib/python2.7/dist-packages', '/home/me/Downloads/neon',
 '/home/me/Downloads/neon/.venv/lib/python2.7',
 '/home/me/Downloads/neon/.venv/lib/python2.7/plat-x86_64-linux-gnu',
 '/home/me/Downloads/neon/.venv/lib/python2.7/lib-tk',
 '/home/me/Downloads/neon/.venv/lib/python2.7/lib-old', 
 '/home/me/Downloads/neon/.venv/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7', 
 '/usr/lib/python2.7/plat-x86_64-linux-gnu', 
 '/usr/lib/python2.7/lib-tk', 
 '/home/me/Downloads/neon/.venv/local/lib/python2.7/site-packages', 
 '/home/me/Downloads/neon/.venv/lib/python2.7/site-packages']



(.venv) me@ARL--M6800:~/Downloads/neon$ pip install pytools 
Requirement already satisfied (use --upgrade to upgrade): pytools in     
 /usr/local/lib/python2.7/dist-packages/pytools-2016.1-py2.7.egg
Requirement already satisfied (use --upgrade to upgrade): 
 decorator>=3.2.0 in /usr/local/lib/python2.7/dist-packages (from pytools)
Requirement already satisfied (use --upgrade to upgrade): appdirs>=1.4.0 
  in /usr/local/lib/python2.7/dist-packages/appdirs-1.4.0-py2.7.egg (from 
  pytools)
Requirement already satisfied (use --upgrade to upgrade): six>=1.8.0 in 
 /usr/local/lib/python2.7/dist-packages (from pytools)
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.6.0 in 
 /usr/local/lib/python2.7/dist-packages (from pytools)

所以,我无法导入 pytools,因为它不在我的 sys.path 上。根据pip,它安装在/usr/local/lib/python2.7/dist-packages/pytools-2016.1-py2.7.egg目录下,这给我留下了3个问题:

首先:为什么我的virtualenv可以看到我的系统包?我以为默认是看不到它们。当我查看用于创建 virtualenv 的 Makefile 时,我看到了这个

  # where our installed python packages will live
  VIRTUALENV_DIR := .venv
  VIRTUALENV_EXE := virtualenv -p python2.7  # use pyvenv for python3 install
  ACTIVATE := $(VIRTUALENV_DIR)/bin/activate

这应该给我默认行为。

其次:为什么我的 dist-packages 目录中有 egg 个目录?这不会让找到这些模块变得更难吗? (虽然显然,我的系统环境 python 的 sys.path 已更新为在 egg 目录中搜索。When/How???)

第三:什么是修复问题的有效方法,以便我的 virtualenv 可以访问 pytools?

(我会用编号代替 First/Second/Third,但我插入的 Makefile 代码格式不正确)

首先,我对Nervana's Neon一无所知。

当使用 root 权限创建 virtualenv 时,我得到相同的 sys.path

dm@Z580:~$ sudo virtualenv test1 -p python2.7
[sudo] password for dm: 
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in test1/bin/python2.7
Also creating executable in test1/bin/python
Installing setuptools, pip, wheel...done.
dm@Z580:~$ source test1/bin/activate
(test1)dm@Z580:~$ which python
/home/dm/test1/bin/python
(test1)dm@Z580:~$ 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 sys
>>> import pprint
>>> pprint.pprint(sys.path)
['',
 '/home/dm/test1/lib/python2.7',
 '/home/dm/test1/lib/python2.7/plat-x86_64-linux-gnu',
 '/home/dm/test1/lib/python2.7/lib-tk',
 '/home/dm/test1/lib/python2.7/lib-old',
 '/home/dm/test1/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/home/dm/test1/local/lib/python2.7/site-packages',
 '/home/dm/test1/lib/python2.7/site-packages']

我会删除当前的 virtualenv,在没有 sudo 的情况下重新创建它,然后使用 virtualenv pip.

安装所有要求
cd ~/Downloads
rm -rf neon/.venv/
virtualenv neon/.venv/ -p python2.7
source neon/.venv/bin/activate
pip install -r neon/requirements.txt

为了它的价值,我在我的主要环境中使用 pip 卸载了 pytools。然后,进入我的虚拟环境并使用 pip 安装 pytools。最后,回到我的主环境,使用 pip install 重新安装了 pytools。

现在一切正常。 (奇怪的是,pytools 不再安装在“.egg”目录中。我认为这样更好,但现在我对 why/when 东西安装到 .egg 目录中感到更加困惑)