[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

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

If you found this article helpful , please give it a like!
3
Loading...
3 votes, average: 1.00 / 13
59
X facebook Hatena Bookmark pocket
[Umeda, Osaka] BytePlus "CDN" / LLM "Skylark" seminar

[Umeda, Osaka] BytePlus "CDN" / LLM "Skylark" seminar

First landing in Kansai! LINE NEXT takes on the challenge! Possibility of the next generation blockchain game PF - with luxurious bonuses -

First landing in Kansai! LINE NEXT takes on the challenge! Possibility of the next generation blockchain game PF - with luxurious bonuses -

The person who wrote this article

About the author

Kawa Ken


A curious Poke○n who belongs to the System Solution Department.