How to embed a Python interpreter in a PyQT widget

Bit late I know, but I recommend the code.InteractiveConsole class: http://docs.python.org/py3k/library/code.html#code.InteractiveConsole


You might look into using threads to keep the UI responsive while printing big loops. This would also help keep your tracebacks clean.

Keeping variables in a dict is the way to go – it's what Python itself does internally. As far as exposing “some, but not all” of them, consider just exposing them all. Much easier. If you're concerned about security, beware that you can't reliably hide anything in Python.

As for the hideous cursor/text manipulation: take advantage of the fact that you have a GUI. With a terminal, you just have one “text box”, but in Qt, it might be more appropriate to have a log/result view and a separate command box.

The log view would display the entered commands and results in a read-only textbox.

The command textbox would allow you to enter a command cleanly.

This approach is used in some web frameworks – e.g. via WebError:

enter image description here