How To
Jul 22, 2024

How to Visualize your Python Project’s Dependency Graph

Understanding your project’s dependency graph is important for a number of reasons:

  • Education ->Visualization helps new developers easily understand how things should be structured and where new functionality should go.
  • Refactoring → Understanding how a certain module is used and what its dependencies are creates a clear path to modifying or removing it.
  • Code Quality → Easily identify circular module level dependencies, poor design decisions, and weak points, such as tightly coupled modules.

With Tach, you can easily visualize the set of dependencies that exist within your Python project.  Here’s how:

1. Install Tach

> pip install tach

The first step is pretty straightforward - install Tach and make sure you’re on the latest version.

2. Define module boundaries

> tach mod

Next, we need to define the modules within your project that we want to visualize dependencies between. The tach mod command opens up an editor in which you can mark modules and your Python source root. Here are the commands you need:

  • up/down/left/right arrow: navigate
  • enter: mark directory or file as module
  • Ctrl + a: Mark all files or directories at the current level as modules
  • s: mark directory as Python source root
  • Ctrl + s: save

We’ll use the FastAPI repo as an example. Here’s what this looks like for FastAPI before saving:

3. Sync your dependencies to Tach

In order to correctly visualize your existing dependencies, Tach will crawl the Python AST to figure out which modules import from each other. This can be achieved by running:

> tach sync

Here’s a slice of what Tach found for the above configuration:

4. View your dependencies with Tach

The final step is the simplest. Simply run tach show --web, and click on the generated URL to view your dependency graph! You can also view a local version of the graph through GraphViz using the base command tach show .

Here’s the link for FastAPI!

By following the above 4 steps, you can easily visualize your project’s set of Python dependencies, and inform future architecture decisions.

Tach also enables you to enforce boundaries between modules, as well as define strict interfaces for a given module. If there’s a feature you’d like to see in Tach, feel free to drop an issue or join the discussion on Discord!