Python: Script for detecting circular import

Does anyone know a / script library that can scan source directories and detect circular import?

+4
source share
1 answer

I don’t know any tool, but there are several ways that I can think of right now to get this data.

Make translator work for you

For each module that you have, create a stub module that imports it, and then run that module with

$ python -v stub_module.py 

This only works if you do not rely on the sys.path hacking, and your modules have no dangerous side effects when importing (both are very dubious FWIW functions). You will get a transit import close, but detecting a circular import with this should be simple.

Use logilab.astng

With logilab.astng, it is easy to extract all the direct import of your modules (find nodes like logilab.astng.nodes.From and logilab.astng.nodes.Import ). Once you have a direct import list of all modules, create import schedules and find the loops.

Again, this only works if you are not using the sys.path hacks.

+2
source

Source: https://habr.com/ru/post/1397117/


All Articles