pyrasite

raw JSON →
2.0 verified Fri May 01 auth: no python

Inject code into a running Python process. Attach a Python shell or run arbitrary code inside an already-running Python interpreter. Current version 2.0. Release cadence is low, last updated 2019.

pip install pyrasite
error OSError: [Errno 1] Operation not permitted
cause ptrace disabled or no permission to attach to the target process.
fix
Run as root or adjust /proc/sys/kernel/yama/ptrace_scope to 0.
error gdb: command not found
cause GDB is not installed on the system.
fix
Install gdb: apt-get install gdb (Debian/Ubuntu) or equivalent.
error pyrasite.exceptions.InjectionError: Could not attach to process
cause Process does not exist, or permissions incorrect, or gdb not found.
fix
Verify PID exists, run as root, install gdb, and set ptrace_scope.
gotcha Requires gdb installed on the system and ptrace enabled (e.g., via yama ptrace_scope).
fix Install gdb (apt-get install gdb) and run: echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
gotcha pyrasite injects code using GDB, which may not be available on minimal Docker containers or Alpine Linux.
fix Use a full base image (e.g., ubuntu), or consider alternative methods like ptrace-based injectors.
breaking Version 2.0 changed the internal API; pyrasite.inject() is now the main entry point, replacing older pyrasite.main or `pyrasite` CLI calls from code.
fix Use pyrasite.inject(pid, code) instead of subprocess calls to pyrasite CLI.
deprecated The CLI tool 'pyrasite' itself is still supported but considered legacy; programmatic use via pyrasite.inject() is preferred.
fix Use the Python API: pyrasite.inject(pid, code)
gotcha Python 2 processes require a different payload tag? Not officially supported in 2.0.
fix Only inject into Python 3 processes; Python 2 target processes may crash.

Injects a simple print statement into the process with given PID. Requires gdb and ptrace permissions.

import os
import pyrasite

pid = os.environ.get('TARGET_PID', '1234')
try:
    pyrasite.inject(pid, 'print("Hello from injected code")')
except Exception as e:
    print(f'Failed: {e}')