{"id":27293,"library":"pyrasite","title":"pyrasite","description":"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.","status":"active","version":"2.0","language":"python","source_language":"en","source_url":"https://github.com/lmacken/pyrasite","tags":["injection","debugging","runtime","gdb","ptrace"],"install":[{"cmd":"pip install pyrasite","lang":"bash","label":"Standard"}],"dependencies":[{"reason":"Required at runtime to attach to processes; must be installed on the system (e.g., apt-get install gdb).","package":"gdb","optional":true}],"imports":[{"note":"PyrasiteTunnel lived in pyrasite.tunnel since 1.0+; direct import breaks.","wrong":"from pyrasite import PyrasiteTunnel","symbol":"PyrasiteTunnel","correct":"from pyrasite.tunnel import PyrasiteTunnel"},{"note":"reverse_connection is in pyrasite.reverse_tunnel.","wrong":"from pyrasite import reverse_connection","symbol":"reverse_connection","correct":"from pyrasite.reverse_tunnel import reverse_connection"}],"quickstart":{"code":"import os\nimport pyrasite\n\npid = os.environ.get('TARGET_PID', '1234')\ntry:\n    pyrasite.inject(pid, 'print(\"Hello from injected code\")')\nexcept Exception as e:\n    print(f'Failed: {e}')","lang":"python","description":"Injects a simple print statement into the process with given PID. Requires gdb and ptrace permissions."},"warnings":[{"fix":"Install gdb (apt-get install gdb) and run: echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope","message":"Requires gdb installed on the system and ptrace enabled (e.g., via yama ptrace_scope).","severity":"gotcha","affected_versions":"all"},{"fix":"Use a full base image (e.g., ubuntu), or consider alternative methods like ptrace-based injectors.","message":"pyrasite injects code using GDB, which may not be available on minimal Docker containers or Alpine Linux.","severity":"gotcha","affected_versions":"all"},{"fix":"Use pyrasite.inject(pid, code) instead of subprocess calls to pyrasite CLI.","message":"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.","severity":"breaking","affected_versions":"2.0"},{"fix":"Use the Python API: pyrasite.inject(pid, code)","message":"The CLI tool 'pyrasite' itself is still supported but considered legacy; programmatic use via pyrasite.inject() is preferred.","severity":"deprecated","affected_versions":"2.0"},{"fix":"Only inject into Python 3 processes; Python 2 target processes may crash.","message":"Python 2 processes require a different payload tag? Not officially supported in 2.0.","severity":"gotcha","affected_versions":"2.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Run as root or adjust /proc/sys/kernel/yama/ptrace_scope to 0.","cause":"ptrace disabled or no permission to attach to the target process.","error":"OSError: [Errno 1] Operation not permitted"},{"fix":"Install gdb: apt-get install gdb (Debian/Ubuntu) or equivalent.","cause":"GDB is not installed on the system.","error":"gdb: command not found"},{"fix":"Verify PID exists, run as root, install gdb, and set ptrace_scope.","cause":"Process does not exist, or permissions incorrect, or gdb not found.","error":"pyrasite.exceptions.InjectionError: Could not attach to process"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}