0

In the official document said the following code

python -m trace --count -C . somefile.py ...

lists all the modules imported during the execution.

However, I couldn't find any documentation about the programmatic API for doing the same thing inside the code. What I am looking for is not How to list imported Modules?. My concern is listing all the imported modules and imported modules of those imported modules and so on, If and only if for any particular module it is imported on execution path.

Community
  • 1
  • 1
ozgur
  • 2,549
  • 4
  • 25
  • 40

1 Answers1

0

If you are looking for which modules are imported at a given time in the environment, then you can use the sys.modules dictionary. There is also a sys.path list, which shows in which directories the interpreter is looking for python modules.

If you need a list of modules being imported by a particular module, then you have to parse the import statements and build a list yourself, recursively (much more work, but doable).

Cyb3rFly3r
  • 1,321
  • 7
  • 12