Skip to content

Python¤

Regular Python¤

Python code is executed in the current process, with isolated global variables.

To capture the output of your code, Markdown Exec patches the print function so that it writes to a buffer instead of standard output.

```python exec="1"
print("**Hello world!**")
```

Hello world!

See the Gallery for more complex examples.

Python console code¤

Code blocks syntax-highlighted with the pycon identifier are also supported. These code blocks will be pre-processed to keep only the lines starting with >>>, and the chevrons (prompt) will be removed from these lines, so we can execute them.

```pycon exec="1" source="console"
>>> print("I'm the result!")
I'm not the result...

```
>>> print("I'm the result!")
I'm the result!

It also means that multiple blocks of instructions will be concatenated, as well as their output:

```pycon exec="1" source="console"
>>> name = "Baron"
>>> print(name)
Baron
>>> age = "???"
>>> print(age)
???

```
>>> name = "Baron"
>>> print(name)
>>> age = "???"
>>> print(age)
Baron
???

Interactive console¤

When the interactive flag is set, pycon blocks will use an alternative formatter that mimics the behavior of the Python REPL interpreter.

In this mode outputs are displayed immediately after their source lines, rather than all outputs being shown at the end. Additionally, outputs from unbound expressions are displayed automatically (although print can still be used as well).

```pycon exec="1" interactive="1"
>>> x = [3, 2, 1]
# Output from unassigned expressions is displayed without needing `print`
>>> sorted(x)
# `print` still works as normal
>>> print(x)

```
>>> x = [3, 2, 1]
>>> sorted(x)
[1, 2, 3]
>>> print(x)
[3, 2, 1]

Exceptions are also handled differently: when a line raises an exception other lines in the same block are still executed and displayed as normal. The raised exception is printed after the generating line and logged as a warning.

```pycon exec="1" interactive="1" id="pycon_error"
>>> y = 2
>>> y / 0
>>> y * 2
```
>>> y = 2
>>> y / 0
Traceback (most recent call last):
  File "<code block: id pycon_error>", line 2, in <module>
    >>> y / 0
        ~~^~~
ZeroDivisionError: division by zero
>>> y * 2
4
WARNING  -  markdown_exec: Unexpected exception while executing pycon code block 'pycon_error':

Traceback (most recent call last):
    File "<code block: id pycon_error>", line 3, in <module>
    >>> y / 0
        ~~^~~
ZeroDivisionError: division by zero