Visualize dependencies in Python projects with pydeps
Hello,
I
'm Kawai from the Circulation Reference System Solutions Department.
It's May. Golden Week
Now, when you're developing something with Python, do you ever run into trouble like, "Which module does this code depend on?" or "I can't understand the structure of the entire project"? (Huh, not?)
Pydeps a useful tool that will help you with such worries .
Using pydeps, you can analyze dependencies between modules in a Python project and visualize them clearly with diagrams.
This article will introduce how to install and use pydeps.
Setup and preparation
■Verification environment
Ubuntu 24.04.2 LTS Python 3.12.3
Install Graphviz for illustration depiction
$ sudo apt update $ sudo apt install graphviz
Create and move the directory for your project anywhere.
This time we will use the standard Python venv. Any version control tool is fine, so you can choose from it.
$ mkdir myproject $ cd myproject $ python3 -m venv env $ source env/bin/activate
Install pydeps
(env)$ pip3 install pydeps
Creating sample code
I'll put some simple code under myproject/.
This is a sample code showing basic dependencies that main.py imports and uses utils.py.
■Configuration
myproject/ ├── main.py └── utils.py
■utils.py
def helper_function(): print("This is a helper function")
■main.py
import utils # import utils.py def main_process(): print("run the main process") utils.helper_function() if __name__ == "__main__": main_process()
If you break down main.py in small detail,
1) It depends on another file called utils.py (import utils),
2) Use helper_function in it (utils.helper_function()),
3) perform the main processing within the main_process function,
4) Start the main_process if it is executed directly as a script (if __name__ == "__main__":)
It is structured like this.
# Run result (env)$ python3 main.py Runs the main process This is a helper function
Try pydeps
I'll try running it on main.py right away.
(env)$ pydeps main.py
▼"main.svg" has been output.
pydeps looks at the line "import utils" and displays the information "main depends on utils" as a diagram.
Incidentally, the contents of a completely different project are completely different, but if there are multiple dependencies, this is the output.
Other options
Additionally, using the options allows you to output in a non-svg format.
■Output to other extensions
# -o option (env)$ pydeps main.py -o dependency.png
■ Output based on json
(env)$ pydeps main.py --show-deps { "main.py": { "bacon": 0, "imports": [ "utils" ], "name": "main.py", "path": null }, "utils": { "bacon": 1, "imported_by": [ "main.py" ], "name": "utils", "path": "/home/hamchan/TEST/myproject/utils.py" } }
As mentioned above, it is possible to output in various formats.
Installation is also extremely easy, so please give it a try
complete