Python Execution Layer

Persistent Python execution inside the current PySH process.

Note: Python execution is a first-class PySH feature. Trusted Python entered through py,#py, or ~/.pyshrc.pyruns with the current user's authority.

Overview

PySH keeps a persistent Python runtime context for the current shell session. Imports and variables created by one-line py, multi-line blocks, and interactive #py can be reused later in the same session. Exceptions are reported without terminating the surrounding PySH shell.

One-line Python — py

py <code> uses exec semantics. Expression results are not automatically echoed, so useprint(...) when output is required.

Persistent one-line Python

$ py print("hello from python")

hello from python

$ py x = 10

$ py print(x)

10

$ py from pathlib import Path; print(Path(".").resolve())

Multi-line blocks — py {

The opener is py {. The closer is a line containing only }, apart from surrounding whitespace. Block bodies share the same persistent runtime as one-line py.

Multi-line Python block

$ py {

import os

paths = os.environ.get("PATH", "").split(":")

print(len(paths))

}

Interactive mode — #py

Enter #py at the normal PySH prompt to start interactive Python command mode. Ctrl+D or#exit returns to the shell.

Interactive Python mode

$ #py

PySH Python Command Execution Layer | GPL-2.0-only

Python <current runtime>

Type #help for commands. Ctrl+D or #exit to return to PySH.


$ x = 10

$ x * 5

50

$ #exit

Interactive directives

  • ✓#exit — return to the normal PySH prompt
  • ✓#help — show directive help
  • ✓#open <file> — load Python source into the buffer
  • ✓#save [file] — save the source buffer
  • ✓#show [file] — display the buffer or a Python file
  • ✓#run — execute the current source buffer
  • ✓#clear — clear the buffer while keeping runtime state
  • ✓#reset — clear buffer, active file, and runtime state
  • ✓#edit / #insert / #replace / #delete — edit the file-backed Python buffer

State persistence

  • ✓Imports and variables persist for the current PySH session
  • ✓The one-line, block, and interactive surfaces share the persistent runtime
  • ✓Runtime state is reset when the PySH session ends
  • ✓Use .py files, .pysh scripts, or plugins for code that must be stored across sessions

Color and editor behavior

Python command mode supports optional Pygments highlighting when the highlight extra is installed. The documented color controls include PYSH_COLOR and NO_COLOR; plain rendering remains functional without Pygments.

PySH script mode is separate

Info: pysh script.pysh arg1 arg2 executes a PySH-native script through the PySH language engine. It is not a shortcut for running arbitrary Python files in the Python Execution Layer.