Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python and bpython using different PYTHONPATHs in Virtualenv

Something strange and unexpected is happening with the sys.path of any virtual environment I set. For example, a clean env:

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

This is the expected 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

But this is the one I really get:

(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

I can't figure out the reason of the two different sys.paths. Because of that, no pip installation works! I'm using Virtualenv 1.7.2, Ubuntu 12.04, Python 2.7.3. Any help will be appreciated.

like image 675
andref Avatar asked Dec 06 '25 08:12

andref


1 Answers

Rather than installing one copy of bpython per virtualenv, I've added this function to my shell profile (for example ~/.bashrc or ~/.zshrc). It wraps the bpython command with some logic to load the virtual environment's python path (if you have an active virtual environment).

bpython() {
    if test -n "$VIRTUAL_ENV"
    then
        PYTHONPATH="$(python -c 'import sys; print ":".join(sys.path)')" \
        command bpython "$@"
    else
        command bpython "$@"
    fi
}
like image 198
Ben Avatar answered Dec 10 '25 22:12

Ben



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!