Import diagram/structure inside a python folder (clean-up code)

Viewed 813

I just finished a middle-sized python (3.6) project and I need to clean it a bit. I am not a software engineer, so during the development, I was not too accurate structuring the project, so now I have several modules that are no (longer) imported by any other module or modules that are imported by other .py files that are not actually needed.

So for example, I have

Project/
├── __init__.py
├── main.py
├── foo.py
|
├── tools/
│   ├── __init__.py
│   ├── tool1.py
│   └── tool2.py
│   └── tool3.py
|
├── math/
│   ├── __init__.py
│   ├── math1.py
│   └── math2.py
├── graph/
│   ├── __init__.py
│   ├── graph1.py
│   ├── graph2.py
│

and inside

main.py

from math import math1
from tools import tool2

graph1.py

from math import math1
from tools import tool1, tool2

foo.py

from tools import tool3

If I could see in one look that not a module imports graph2 or math2, I could delete them, or at least add them as candidates for deletion (and restructure the project in a better way). Or I may think to delete tool3 because I know I don't need foo anymore.

Is there an easy way to visualize all the "connections" (which module imports which) in a diagram or some other kind of structured data/visualization manner?

3 Answers

You can use Python to do the work for you:

Place a Python file with the following code into the same directory as your Project directory.

from pathlib import Path

# list all the modules you want to check:
modules = ["tool1", "tool2", "tool3", "math1", "math2", "graph1", "graph2"]

# find all the .py files within your Project directory (also searches subdirectories):
p = Path('./Project')
file_list = list(p.glob('**/*.py'))

# check, which modules are used in each .py file:
for file in file_list:
    with open(file, "r") as f:
        print('*'*10, file, ':')
        file_as_string = f.read()
        for module in modules:
            if module in file_as_string:
                print(module)

Running this will give you an output looking something like this:

********** Project\main.py :
tool1
tool2
graph1
********** Project\foo.py :
tool2
********** Project\math\math1.py :
tool2
math2

If you're in a Unix-like platform (such as macOS), you can find all files containing specific text with grep. So you could search for all files containing ''import math1'' in your Project directory, for example, with grep -rnw '/path/to/Project/' -e 'import math1' , and if there are no results, then you can safely remove the module. All this process can be easily automated with a python or a shell script!

Maybe this project can help you with visualizing your dependency graph. After a quick google search, it looks like you're not the first person to try to do this.

Related