Sentry Answers>Python>

Trace function execution in large Python projects

Trace function execution in large Python projects

David Y.

The ProblemJump To Solution

I have a large Python project with many different inter-related functions and module files. I would like to see which functions are being executed in what order without having to add print statements everywhere. What would be the best and simplest way to accomplish this?

The Solution

Two of the best tools for viewing function execution order in a large project are Hunter and VizTracer. Both are easy to use and produce an appealing visual output of the function execution order in a given project.

To demonstrate this, let’s consider the following toy project consisting of three Python files:,, and

Click to Copy
# from lib_one import e def a(): b() def b(): c() def c(): d() def d(): e() a()
Click to Copy
# from lib_two import g def e(): f() def f(): g()
Click to Copy
# def g(): h() def h(): print("Hello!")

This project calls several different functions across all three files. If we run, it will print the following:

Click to Copy

Below, we’ll visualize this code’s execution with both VizTracer and Hunter.


To produce a graphical, interactive visualization of the code’s execution without adding any additional code, we can install and run VizTracer:

Click to Copy
pip install viztracer viztracer

This will produce output something like the following:

Click to Copy
hello Total Entries: 405 Use the following command to open the report: vizviewer /tmp/result.json

Execute the vizviewer command above to open a browser window showing an interactive tracing graph like the one shown in the project’s README. Click on different parts of the graph to see more details in the bottom panes.


If VizTracer provides too much complexity and unneeded functionality, we can use Hunter instead, to show a simple, terminal-based diagram of our code’s execution.

First, install it with PIP:

Click to Copy
pip install hunter

Then, add the following lines to the top of

Click to Copy
import hunter # new import hunter.trace() # start tracing from lib_one import e def a(): b() # ...

Now run python You should see output similar to that below:

Click to Copy
/tmp/ call => a() /tmp/ line b() /tmp/ call => b() /tmp/ line c() /tmp/ call => c() /tmp/ line d() /tmp/ call => d() /tmp/ line lib_one.e() /tmp/ call => e() /tmp/ line f() /tmp/ call => f() /tmp/ line lib_two.g() /tmp/ call => g() /tmp/ line h() /tmp/ call => h() /tmp/ line print("Hello!") Hello! /tmp/ return <= h: None /tmp/ return <= g: None /tmp/ return <= f: None /tmp/ return <= e: None /tmp/ return <= d: None /tmp/ return <= c: None /tmp/ return <= b: None /tmp/ return <= a: None

This will be followed by additional tracing of Python’s shutdown process and the Hunter library itself. To suppress the tracing of functions from Python’s built-in libraries, we can add stdlib=False as a keyword argument to hunter.trace(). More information is provided in the official documentation.

As an alternative to changing our code to use Hunter, make Python import it automatically by setting the PYTHONHUNTER environment variable before execution:

Click to Copy

This will have the same effect as adding those two Hunter lines above, even if the environment variable is set to an empty string. We can put any keyword arguments we want to pass to hunter.trace in PYTHONHUNTER. For example:

Click to Copy
PYTHONHUNTER="stdlib=False" python

This is equivalent to:

Click to Copy
import hunter hunter.trace(stdlib=False)
  • Sentry BlogPython Performance Testing: A Comprehensive Guide
  • Sentry BlogLogging in Python: A Developer’s Guide
  • logo
    Listen to the Syntax Podcast

    Tasty Treats for Web Developers brought to you by Sentry. Web development tips and tricks hosted by Wes Bos and Scott Tolinski

    Listen to Syntax

Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.