It is sometimes useful to access the source of an external Python package in your current virtualenv, for example to inspect its code or to place breakpoints while debugging.
If you use
virtualenv_wrapper then you may use the
cdsitepackages command. However, this doesn’t help much for packages that have been installed in “editable” mode (that is, typically in the
So I wrote a little bash & zshell script to quickly access any package, regardless of where they are installed in your virtualenv. This script also allows tab completion to make things even faster:
(myenv)$ goto <TAB> admin_tools curses encodings feincmstools lib2to3 paramiko ratelimit appconf dateutil example gargoyle logging password_reset requests bsddb debug_toolbar example_module gunicorn markdown PIL setuptools compiler distutils example_project hotshot modeldict pip sorl compressor django fabfile idlelib mptt psycopg2 south ctypes email feincms jsonfield nexus pyrepl (myenv)$ goto d<TAB> dateutil debug_toolbar distutils django (myenv)$ goto django (myenv)$ pwd /home/vagrant/.virtualenvs/myenv/lib/python2.6/site-packages/django
Here’s the source:
To install this script in your user’s
bin/ folder, simply run the following:
(myenv)$ mkdir -p ~/bin/ (myenv)$ wget https://gist.github.com/jphalip/5967635/raw/goto.sh -P ~/bin/ (myenv)$ source ~/bin/goto.sh
To make this script accessible in all future terminal sessions, simply add
source ~/bin/goto.sh to your user’s bash or zshell profile.
PS: Thanks to Carlos Nepomuceno for providing some useful tips about
pkgutil on the Python users mailing list.