Lightweight replacement for os.system() with a persistent environment.
persishell is a simple Python library that wraps a persistent Bash subprocess, allowing you to run shell commands sequentially — with shared environment state across runs. It's useful when you need to maintain exported variables, cd into directories, interact with module load, or configure shell settings once and use them across multiple commands.
I use it extensively on SLURM clusters to write all my experiment scripts (including the sbatch scripts) in Python.
pip install persishellOr for development:
pip install -e .from persishell import PersiShell
sh = PersiShell()
# Set environment variables
sh.export("FOO", "bar")
# Commands use the persistent environment
sh.run("echo $FOO") # prints: bar
# Change directories
sh.run("cd /tmp")
sh.run("pwd") # prints: /tmp
# Unset environment variables
sh.unset("FOO")
sh.run("echo $FOO") # prints an empty linePersiShell.run(command: str | list, optinal arguments)Run a command inside the persistent shell. Accepts a string or a list of arguments.
PersiShell.export(key: str, value: str)Export a persistent environment variable.
PersiShell.unset(key: str)Unset a previously defined environment variable.
Currently designed for Unix-like systems (uses bash, fcntl). It has not been tested on Windows.
MIT License