Skip to main content

pycmdrun

Python License PyPI

Run shell commands with persistent directory context, full logging, color output, and log export.


Installation

pip install pycmdrun

With color support:

pip install pycmdrun[color]

Quick start

from pycmdrun import cmd

cmd.cd(r"C:\Users\Fede\Desktop")
cmd.run("echo Hello from pycmdrun")
cmd.run(["pip", "install", "numpy"])   # list form also works

print(cmd.log)
cmd.save_log("session.json", fmt="json")

API

cmd.cd(path) → self

Changes the working directory for all subsequent run() calls.

  • Expands ~ and environment variables
  • Raises ValueError if the directory does not exist
  • Returns self so it can be chained: cmd.cd("/tmp").run("ls")

cmd.run(command, *, timeout, encoding, raise_on_error, env) → CommandResult

Executes a command in the current working directory.

Parameter Type Default Description
command str or list[str] — Command to run
timeout float None Kill after N seconds
raise_on_error bool False Raise RuntimeError on non-zero exit
env dict None Extra environment variables

CommandResult attributes:

Attribute Type Description
.stdout str Standard output
.stderr str Standard error
.returncode int Exit code (0 = success)
.ok bool True when returncode == 0
.cwd str Directory where the command ran

cmd.log (property)

Returns a formatted string with the full history of every cd() and run() call.

[1] 2026-01-15 10:30:00  |  cwd: /tmp
    $ echo Hello
    exit: 0
    stdout:
    Hello

cmd.save_log(path, *, fmt="txt")

Exports the log to a file. fmt can be "txt" or "json".

cmd.save_log("session.txt")
cmd.save_log("session.json", fmt="json")

cmd.log_entries (property)

Raw log as a list[dict] for programmatic access. Each entry has: timestamp, command, stdout, stderr, returncode, cwd.


cmd.clear_log()

Clears all log entries.


cmd.cwd (property)

Returns the current working directory.


Verbose mode

Prints each command and its result in real time as it runs:

from pycmdrun import CommandRunner

cmd = CommandRunner(verbose=True)
cmd.cd("/tmp")
cmd.run("echo Hello")

Output:

  → cwd: /tmp
  $ echo Hello
  ✓ exit: 0
    Hello

Color support

Install colorama to get colored output in the log and verbose mode:

pip install pycmdrun[color]
# or
pip install colorama

When saving logs to file with save_log(), ANSI color codes are automatically stripped.


License

MIT © Fede

Metadata

Release files for pycmdrun 1.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pycmdrun 1.2.2
File Size Uploaded Provenance Actions
pycmdrun-1.2.2.tar.gz 8.0 kB Without provenance Details for pycmdrun-1.2.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pycmdrun 1.2.2
File Interpreter ABI Platform Actions
pycmdrun-1.2.2-py3-none-any.whl Python 3 none any Details for pycmdrun-1.2.2-py3-none-any.whl

Total release size: 15.8 kB

Release files / pycmdrun-1.2.2.tar.gz

Download URL pycmdrun-1.2.2.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
76c02144b58620cda37901d5285d743f2f00adbfa66290baaf91a01b234bdf87
BLAKE2b-256 checksum
How to use checksums
8cd7f2c58a416ee0eb31dffbeb0026717a19b255c8c329cd357545d666a484e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.0

Release files / pycmdrun-1.2.2-py3-none-any.whl

Download URL pycmdrun-1.2.2-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
db227add24b0f3ccab1b180c1ffde743bd2faa9abf1a0b234de622120a39e9cd
BLAKE2b-256 checksum
How to use checksums
9d983d0e78493e9de5b27b1bb1f75131eb7bc57fb19e884c5a20bb8deed05fae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.0

Security for pycmdrun 1.2.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page