---
title: "minipy"
---
`execnb.minipy` brings IPython syntax to ordinary Python programs without requiring IPython or a Jupyter kernel. Its interactive shell, adapted from IPython, runs Python code, `%` magics, and `!` shell commands, and supports rich display. You can also use its input transformer to translate IPython syntax to Python without executing it. Install it with `pip install execnb`.

## Get a Python result

Use a bare [`InteractiveShell`](minipy/interactiveshell.md#interactiveshell) in a script or test when you want the value of an expression. Each shell keeps its own namespace across cells.

```python
from execnb.minipy.interactiveshell import InteractiveShell

ip = InteractiveShell()
ip.run_cell("x = 6")
r = ip.run_cell("x * 7")
r.raise_error()
assert r.result == 42
```

`run_cell` prints the result as an interactive shell would. It records exceptions on the returned object; `raise_error()` re-raises them. For code that suspends, use `await fastcore.nbio.run_cell(ip, source)` on your event loop.

## Capture notebook output

In a fresh Python process:

```python
from execnb.shell import CaptureShell

sh = CaptureShell(use_ipython=False)
out = sh.run("from IPython.display import Markdown\ndisplay(Markdown('**Ready**'))")
assert out[0]['data']['text/markdown'] == ['**Ready**']
sh.close()
```

`sh.run` returns notebook output records for streams, rich displays, expression results, and errors. It also accepts cells with top-level `await` from synchronous code. Minipy supports `!` shell commands and magics such as `%timeit`, `%%writefile`, and `%matplotlib inline` (with Matplotlib installed). Run `%lsmagic` in a cell to list the available magics. To inspect notebook commands without executing them, see [Parsing notebook commands](index.md#parsing-notebook-commands).

## Choose a backend

[`CaptureShell()`](shell.md#captureshell) uses IPython if installed and minipy otherwise. To select minipy when both are installed, pass `use_ipython=False` before anything imports IPython. All [`CaptureShell`](shell.md#captureshell) instances in a process share that choice. In a Jupyter kernel, IPython is already imported; start a fresh Python process to select minipy.

After minipy is selected, `IPython.display` is available to notebook cells and imported libraries. Other IPython subpackages, extension loading, and persistent input/output history are not provided. For notebooks that use `%load_ext`, or libraries that import `IPython.core`, run `pip install 'execnb[ipython]'` and pass `use_ipython=True`.
