2

我设置的任何虚拟环境的 sys.path 都发生了一些奇怪和意想不到的事情。例如,一个干净的环境:

$ virtualenv test
$ source test/bin/activate
(test) $

这是预期的 PYTHONPATH:

(test) $ python
>>> import sys
>>> print '\n'.join(sys.path)

/home/user/test/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
/home/user/test/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg
/home/user/test/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
/home/user/test/lib/python2.7/site-packages/pip-1.1-py2.7.egg
/home/user/test/lib/python2.7
/home/user/test/lib/python2.7/plat-linux2
/home/user/test/lib/python2.7/lib-tk
/home/user/test/lib/python2.7/lib-old
/home/user/test/lib/python2.7/lib-dynload
/usr/lib/python2.7
/usr/lib/python2.7/plat-linux2
/usr/lib/python2.7/lib-tk
/home/user/test/local/lib/python2.7/site-packages
/home/user/test/lib/python2.7/site-packages

但这是我真正得到的:

(test) $ bpython
>>> import sys
>>> print '\n'.join(sys.path)

/usr/bin
/usr/lib/python2.7
/usr/lib/python2.7/plat-linux2
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages

我无法弄清楚这两个不同 sys.paths 的原因。因此,没有 pip 安装工作!我正在使用 Virtualenv 1.7.2、Ubuntu 12.04、Python 2.7.3。任何帮助将不胜感激。

4

4 回答 4

8

我没有为每个 virtualenv 安装一个 bpython 副本,而是将此函数添加到我的 shell 配置文件中(例如~/.bashrc~/.zshrc)。它使用一些逻辑包装 bpython 命令以加载虚拟环境的 python 路径(如果您有一个活动的虚拟环境)。

bpython() {
    if test -n "$VIRTUAL_ENV"
    then
        PYTHONPATH="$(python -c 'import sys; print ":".join(sys.path)')" \
        command bpython "$@"
    else
        command bpython "$@"
    fi
}
于 2014-03-04T20:33:46.193 回答
8

我发现我需要在安装 bpython 后停用并重新激活我的 virtualenv 才能使其工作。

  1. pip install bpython
  2. deactivate
  3. . bin/activate# 或等效的激活命令
于 2014-07-09T17:43:04.107 回答
6

我的假设是您在激活新的 virtualenv 后还没有安装 bpython。

我完全按照您提到的方式跟进了它:

mkvirtualenv bpython
(bpython)~ $ pip install bpython
(bpython)~ $bpython

然后运行命令:

>>> import sys
>>> print '\n'.join(sys.path)

/Users/xxxx/.virtualenvs/bpython/bin
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/site-packages/pip-1.1-py2.7.egg
/Users/xxxx/.virtualenvs/bpython/lib/python27.zip
/Users/xxxx/.virtualenvs/bpython/lib/python2.7
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/plat-darwin
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/plat-mac
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/plat-mac/lib-scriptpackages
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/lib-tk
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/lib-old
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/lib-dynload
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/site-packages

并通过在激活的 virtualenv 下运行 python 再次做同样的事情

(bpython)~ $ python
.....
>>> import sys
>>> print '\n'.join(sys.path)

/Users/xxxx/.virtualenvs/bpython/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/site-packages/pip-1.1-py2.7.egg
/Users/xxxx/.virtualenvs/bpython/lib/python27.zip
/Users/xxxx/.virtualenvs/bpython/lib/python2.7
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/plat-darwin
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/plat-mac
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/plat-mac/lib-scriptpackages
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/lib-tk
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/lib-old
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/lib-dynload
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/site-packages

我发现这两个结果没有区别

于 2012-06-23T19:51:03.393 回答
2

我还发现,如果您在本地安装了 bpython,则需要创建您的 virtualenv 以--no-site-packages使其正常工作。如果您在没有该标志的情况下创建了 virtualenv,则可以创建一个名为no-global-site-packages.txtin的空文件,~/.virtualenvs/<env-name>/lib/python2.7/Stack Exchange answer中所述。

于 2013-07-25T15:59:51.037 回答