Categories
Python

Embed a Python REPL in your program

So you love the Django shell? The interactive prompt that has your Django stuff loaded and ready for some quick prototyping? You always wanted to embed something similar in your Python apps too? Well, it’s painlessly simple with the “code” module. It offers us the “InteractiveConsole” class which we can extend to quickly get a REPL which is basically the Python REPL with our customizations. The code is heavily documented. Going through it should be adequate to understand whats going on:

What did we do? We subclassed InteractiveConsole to create our own implementation. And then ran it 🙂

Test run outputs: