A simple wrapper around subprocess with persistent environment variables
Project description
persishell
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.
Installation
pip install persishell
Or for development:
pip install -e .
Quick Start
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 line
API Summary
PersiShell.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.
Limitations
Currently designed for Unix-like systems (uses bash, fcntl). It has not been tested on Windows.
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file persishell-0.1.1.tar.gz.
File metadata
- Download URL: persishell-0.1.1.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8078dc3445f3fc38262a1c420bfbf99b283f1567195c7519efbffea83a9bc279
|
|
| MD5 |
20383f5ff5553adc698960f82414147f
|
|
| BLAKE2b-256 |
c7dd2e778eb44002b774e05be633b5decdfcfb75e4a4e6a74d72fae9734d6cb9
|
File details
Details for the file persishell-0.1.1-py3-none-any.whl.
File metadata
- Download URL: persishell-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e0a06ecf6613ae97e5989207c0b2ef43a98e0f49f330b627fa29a4b91c6f6b8a
|
|
| MD5 |
b25947eeebbe38623d5f4fc865c5ae29
|
|
| BLAKE2b-256 |
593249015009c18b3b729fee1827e150f8c44676e815080d2ff61871e44deb70
|