Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Inject code into a running Python process

Project description

Injects code into a running Python process.



Download the latest stable release from PyPi:

easy_install pyrasite

Grab the latest source by running:

git clone git://

You can also fork pyrasite on GitHub:


This pyrasite unit test injects a print(“Hello World!”) payload into a process and ensures it gets printed.

from pyrasite.inject import CodeInjector

def test_injection(self):
    cmd = 'python -c "import time; time.sleep(0.5)"'
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)

    ci = CodeInjector(, 'payloads/')

    stdout, stderr = p.communicate()
    assert 'Hello World!' in stdout, "Code injection failed"


Reverse Python Shell

This lets you easily introspect or alter any objects in your running process.

$ python
>>> x = 'foo'
$ pyrasite <PID> payloads/
$ nc -l localhost 9001
Python 2.7.1 (r271:86832, Apr 12 2011, 16:15:16)
[GCC 4.6.0 20110331 (Red Hat 4.6.0-2)]
Type 'quit' to exit.
>>> print x
>>> globals()['x'] = 'bar'

Viewing the largest objects in your process

This payload uses meliae to dump all of the objects in your process to an objects.json file (currently dumped in the working directory of your process).

$ pyrasite <PID> payloads/

Pyrasite also provides a tool to view the values of largest objects in your process.

$ pyrasite-memory-viewer <PID> objects.json

Reverse Shell

$ pyrasite <PID> payloads/
$ nc -l localhost 9001
Linux tomservo #1 SMP Tue Aug 16 04:10:59 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux
Type 'quit' to exit.
% ls

Call Graph

Pyrasite comes with a payload that generates an image of your processes call graph using pycallgraph.

$ pyrasite <PID> payloads/
$ pyrasite <PID> payloads/

The callgraph is then generated using graphviz and saved to callgraph.png. You can see an example callgraph here.

Dumping modules, thread stacks, and forcing garbage collection


Additional installation notes

Mac OS X

If you don’t want to override Apple’s default gdb, install the latest version of gdb with a prefix (e.g. gnu)

$ ./configure --program-prefix=gnu
$ pyrasite <PID> payloads/ --prefix="gnu"


Since version 10.10, Ubuntu ships with a controversial patch that restricts the scope of ptrace, which can be disabled by running:

echo 0 > /proc/sys/kernel/yama/ptrace_scope


Luke Macken <>

David Malcolm <>

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for pyrasite, version 1.0
Filename, size File type Python version Upload date Hashes
Filename, size pyrasite-1.0.tar.gz (19.7 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page