ptpython: Advanced Python REPL
ptpython is an advanced, cross-platform Python REPL (Read-Eval-Print Loop) built on top of the prompt_toolkit library. It provides features like syntax highlighting, autocompletion, multiline editing, mouse support, and customizable key bindings (Emacs and Vi modes). Currently at version 3.0.32, it sees fairly frequent updates, often with several patch releases per month.
Warnings
- breaking Python 3.7 support was dropped in version 3.0.30. Users on Python 3.7 or older must use an earlier ptpython version (e.g., ptpython 2.0.5 for Python < 3.6, or a 2.x version for Python 3.7).
- gotcha When embedding the REPL using `ptpython.repl.embed()`, configuration files (e.g., `~/.config/ptpython/config.py`) are ignored by default. This can lead to unexpected behavior if you rely on custom settings.
- breaking Since version 3.0.24, the `--asyncio` flag is explicitly required to activate the asyncio REPL when running `ptpython` from the command line. Previously, asyncio mode might have been implicitly active under certain conditions.
- gotcha Version 3.0.28 introduced a custom `exit` function that can be called without parentheses (i.e., just `exit`) to return from the REPL when embedded. While convenient, users accustomed to always calling `exit()` might find this behavior unexpected.
- gotcha In version 3.0.32, the default code theme ('default-ansi') switched to using ANSI colors instead of RGB. This can alter the appearance of syntax highlighting depending on your terminal emulator's color configuration, potentially affecting contrast.
Install
-
pip install ptpython -
pip install ptpython[ptipython]
Imports
- embed
from ptpython.repl import embed
- run_config
from ptpython.repl import run_config
Quickstart
import os
from ptpython.repl import embed
def main():
print("Entering embedded ptpython REPL...")
my_variable = "Hello from host script"
# Pass globals() and locals() to make variables accessible in the REPL
embed(globals(), locals())
print("Exited embedded ptpython REPL.")
if __name__ == "__main__":
# Or simply run from command line:
# $ ptpython
main()