Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python : counting module imports?

I am a mid end python developer at an animation studio, and have been presented with a unique diagnostics request ;

To assess what code gets used and what doesn't.

Within the sprawling disorganized structure of Python modules importing modules : I need to count the python modules that are imported, and possibly at a deeper level, find which methods are called.

As far as finding out which methods are called, I think that would be easy to solve by writing my own logging metaclass.

However, I'm at loss to imagine how I should count or log module imports at varying depths.

Thanks for any ideas you may have.

like image 461
jorxster Avatar asked Aug 26 '14 00:08

jorxster


People also ask

How many times is a module imported Python?

They are executed only the first time the module name is encountered in an import statement. 1 (They are also run if the file is executed as a script.) Each module has its own private namespace, which is used as the global namespace by all functions defined in the module.

How many times does a module get loaded when imported multiple times?

A module code is evaluated only the first time when imported. If the same module is imported into multiple other modules, its code is executed only once, upon the first import. Then its exports are given to all further importers. The one-time evaluation has important consequences, that we should be aware of.


1 Answers

If you have a way to exercise the code, you can run the code under coverage.py. It's normally used for testing, but its basic function would work here: it indicates what lines of code are run and what are not.

like image 128
Ned Batchelder Avatar answered Nov 02 '22 12:11

Ned Batchelder