Quickly open the source of a Python module from the command-line

Add this to your .bashrc:

pysource() {
    $EDITOR $(python -c "import ${1}; import inspect; print inspect.getsourcefile(${1})")
}

Your favorite editor should pop up and you can browse the module source. If you want to open a module from an existing editor window, there’s probably plugins for that.

Then after reloading your .bashrc just run something like:

$ pysource logging

This entry was posted in Programming and tagged , . Bookmark the permalink.
  • Anonymous

    Is there any way to make it work with eggs?

    mbp-luke:~ luke$ pysource genshi.template.base
    -bash: /Library/Python/2.6/site-packages/Genshi-0.6-py2.6.egg/genshi/template/base.py: Not a directory

  • http://www.kamilkisiel.net Kamil Kisiel

    Sounds like your $EDITOR isn’t set ?

  • Anonymous

    Oops, that was a badly sanitized example, but the problem remains that I can’t open a module inside an egg, because the path it returns doesn’t exist in the filesystem.


    mbp-luke:~ luke$ export EDITOR=`which vim`
    mbp-luke:~ luke$ pysource genshi.template.base
    Opens a blank vim editor saying:
    "/Library/Python/2.6/site-packages/Genshi-0.6-py2.6.egg/genshi/template/base.py" [New DIRECTORY]

    or…


    mbp-luke:~ luke$ export EDITOR=`which cat`
    mbp-luke:~ luke$ pysource genshi.template.base
    cat: /Library/Python/2.6/site-packages/Genshi-0.6-py2.6.egg/genshi/template/base.py: Not a directory

    Are some editors able to crack open an egg and get at the files inside, as in a path like this?

blog comments powered by Disqus