python和bpython在Virtualenv中使用不同的PYTHONPATH

and*_*ref 2 virtualenv pythonpath

我设置的任何虚拟环境的sys.path都会发生奇怪和意外的事情.例如,一个干净的环境:

$ virtualenv test
$ source test/bin/activate
(test) $
Run Code Online (Sandbox Code Playgroud)

这是预期的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
Run Code Online (Sandbox Code Playgroud)

但这是我真正得到的:

(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
Run Code Online (Sandbox Code Playgroud)

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

Ben*_*Ben 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
}
Run Code Online (Sandbox Code Playgroud)


erj*_*ang 8

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

  1. pip install bpython
  2. deactivate
  3. . bin/activate #或等效的激活命令


pyf*_*unc 6

我的假设是你在激活新的virtualenv后没有安装bpython.

我跟着它完全像你提到的那样:

mkvirtualenv bpython
(bpython)~ $ pip install bpython
(bpython)~ $bpython
Run Code Online (Sandbox Code Playgroud)

然后运行命令:

>>> 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
Run Code Online (Sandbox Code Playgroud)

并通过在激活的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
Run Code Online (Sandbox Code Playgroud)

我看到两个结果没有区别