The Interactive Interpreter category page had no intro, showing only the table with a generic meta description. Co-Authored-By: Claude <noreply@anthropic.com>
4.2 KiB
At a terminal, IPython replaces the built-in Python interactive interpreter; in a notebook, it runs in Jupyter Notebook, unless you pick marimo for .py files.
How to choose:
- A better shell in the terminal: IPython
- Notebooks in the browser, in Python or other languages: Jupyter Notebook
- Reactive notebooks saved as
.pyfiles, runnable as scripts or apps: marimo - A terminal REPL that flags syntax errors before running: ptpython
- A shell inside your own program: IPython or ptpython
IPython aims to be an interactive shell superior to Python's default, with tab completion, object introspection, system shell access, and command history across sessions. Install it with pip install ipython and start it with ipython. Type object? for details about an object, and object?? for more. %run runs a script and loads its variables into your session, rereading the file each time. After an exception, %debug drops you into pdb to look around.
Jupyter Notebook is a simplified notebook authoring application. A notebook is a shareable document that mixes code, text, charts, and interactive controls. Install it with pip install notebook and run jupyter notebook, which opens it in your web browser. Work on a problem in pieces, organizing related ideas into cells and moving on once earlier ones work. Code runs in a kernel, and kernels for other languages let you write notebooks in more than Python. Notebooks save as JSON files with the .ipynb extension.
marimo is a reactive Python notebook: run a cell, and marimo runs the cells that depend on it, keeping code and outputs consistent. Delete a cell and marimo removes its variables from memory, so no hidden state is left behind. Each notebook is a pure Python file you can version with Git or run as a script with python your_notebook.py. Serve it as an app with marimo run, which hides the code. Install it with pip install marimo, then create or edit a notebook with marimo edit your_notebook.py. To bring a Jupyter notebook over, run marimo convert your_notebook.ipynb -o your_notebook.py. To know what order to run cells in, marimo lets you define each variable in only one cell.
ptpython is an advanced Python REPL with syntax highlighting, multiline editing, autocompletion, and both Vi and Emacs key bindings. Before it runs your input, it checks that it's valid Python and moves the cursor to the error. Install it with pip install ptpython and start it with ptpython. With IPython installed, ptipython adds IPython's magic functions and shell integration.
Both terminal shells can open inside your own program, with access to its variables: call IPython.embed() where you want to look around, or ptpython's embed(globals(), locals()). IPython is also the default Python kernel in Jupyter Notebook.