Get to Python source code instantly using importlib and inspect

Bob, Mon 14 December 2020, Tools

argparse, bash, importlib, inspect, pathlib, pydoc, source code, Standard Library, vim

Have you ever wondered how to get Python source code quickly? It turns out the Standard Library can do this pretty effortlessly. Here is some code to read more Python source.

The code

The source / project is here:

import argparse
import importlib
import inspect
import pydoc

Here is where the magic happens (credit to this Stack Overflow thread): we use importlib to import the module from a string and get the class or function from this imported module using getattr:

# src/
def get_callable(arg):
    module_str, name = arg.rsplit(".", 1)
    module = importlib.import_module(module_str)
    return getattr(module, name)

Then we print it out. By default we use print, but if pager is True we use pydoc.pager which works like Unix more: it waits for you to press spacebar and you can use / for searching / string matching, pretty cool!

# src/
def print_source(func, pager=False):
    output = pydoc.pager if pager else print

To make it stick, check out another example of importlib.import_module and here is another example of inspect.

Lastly in src/ we handle command line arguments. We require a module(.submodule).name for which we want to see the source and we have an optional pager argument. Then we call the two functions:

# src/
def main():
    parser = argparse.ArgumentParser(description='Read Python source.')
    parser.add_argument("-m", "--module", required=True, dest='module',
    parser.add_argument("-p", "--pager", action='store_true', dest='use_pager',
                        help='page output (like Unix more command)')
    args = parser.parse_args()

    func = get_callable(args.module)
    print_source(func, pager=args.use_pager)

(I used sys.argv in my first iteration, but argparse makes this so much more cleaner and extensible! Of course there are more options as well ...)

Calling it from within Vim

I stored this script in my $HOME/bin folder and Michael was so kind to come up with an alias to use it in Vim:

autocmd FileType python map <leader>py :exec '!python3.9 $HOME/bin/ -m <C-R><C-A> -p'<CR>

He even made this nice little gif demo how it calls the script when you press <leader>py when you are on module.class or module.function:

demo of this script

Here we actually wanted to know how pathlib did operator overloading (answer: it implements __truediv__).

Of course you can also just call it from the command line:

$ pysource -m re.match
def match(pattern, string, flags=0):
    """Try to apply the pattern at the start of the string, returning
    a Match object, or None if no match was found."""
    return _compile(pattern, flags).match(string)

Or when using paging:

$ pysource -m pathlib.PurePath -p


I turned it into a package and uploaded it to PyPI (if you're curious how you can do this, check out this article).

By the way, lesson learned: call your source folder something relevant because that is how it ends up in your virtual environment's site-packages folder.

Try it out yourself! Just install it like pip install pybites-pysource (not to be confused with pysource which is another / unrelated package on PyPI, thanks Nils).

This is still experimental but I hope it will already make it easier for you to read more Python (Standard Library) code.

Keep Calm and Code in Python!

-- Bob

PyBites Python Tips

Do you want to get 250+ concise and applicable Python tips in an ebook that will cost you less than 10 bucks (future updates included), check it out here.

Get our Python Tips Book

"The discussions are succinct yet thorough enough to give you a solid grasp of the particular problem. I just wish I would have had this book when I started learning Python." - Daniel H

"Bob and Julian are the masters at aggregating these small snippets of code that can really make certain aspects of coding easier." - Jesse B

"This is now my favourite first Python go-to reference." - Anthony L

"Do you ever go on one of those cooking websites for a recipe and have to scroll for what feels like an eternity to get to the ingredients and the 4 steps the recipe actually takes? This is the opposite of that." - Sergio S

Get the book