Sometimes, we want to list imported modules with Python.
In this article, we’ll look at how to list imported modules with Python.
How to list imported modules with Python?
To list imported modules with Python, we can use the modulefinder
module.
For instance, we write
from modulefinder import ModuleFinder
finder = ModuleFinder()
finder.run_script("myscript.py")
for name, mod in finder.modules.items():
print(name)
to create a ModuleFinder
object.
Then we call run_script
on the object with the path of the script that we want to list the imported modules for.
Then we loop through the dict returned with the module name as the keys with a for loop.
In it, we print the name
of the imported module traversed by the loop.
Conclusion
To list imported modules with Python, we can use the modulefinder
module.