How to List Imported Modules

How to list imported modules?

import sys
sys.modules.keys()

An approximation of getting all imports for the current module only would be to inspect globals() for modules:

import types
def imports():
for name, val in globals().items():
if isinstance(val, types.ModuleType):
yield val.__name__

This won't return local imports, or non-module imports like from x import y. Note that this returns val.__name__ so you get the original module name if you used import module as alias; yield name instead if you want the alias.

How to list imported modules and version in Python3

This worked for me:

import sys
for module in sys.modules:
try:
print(module,sys.modules[module].__version__)
except:
try:
if type(modules[module].version) is str:
print(module,sys.modules[module].version)
else:
print(module,sys.modules[module].version())
except:
try:
print(module,sys.modules[module].VERSION)
except:
pass

How to list all functions in a python module when imported with *

Generally you are rarely recommended to use the from ... import * style, because it could override local symbols or symbols imported first by other modules.

That beeing said, you could do

symbols_before = dir()
from myutils.user_data import *
symbols_after = dir()
imported_symbols = [s for s in symbols_after if not s in symbols_before]

which stores the freshly imported symbols in the imported_symbols list.
Or you could use the fact, that the module is still loaded into sys.modules and do

import sys
from inspect import getmembers, isfunction
from myutils.user_data import *
functions_list = getmembers(sys.modules['myutils.user_data'], isfunction)
print(functions_list)

List imported modules from an imported module in Python 3

Python3 lets us pull in the module with exec(f'import {module_name}'), putting the result in globals()[module_name],
or we can assign mod = importlib.import_module(module_name).

To see what other modules were directly pulled in by that, use:

def is_module(x):
return str(type(x)) == "<class 'module'>"

def show_deps(mod):
for name in dir(mod):
val = getattr(mod, name)
if is_module(val):
print(name, val.__file__)

One could recurse through the tree to find transitive deps, if desired.

List project modules imported both directly and indirectly

You can use modulefinder to run a script and inspect the imported modules. These can be filtered by using the __file__ attribute (given that you actually import these modules from the file system; don't worry about the dunder attribute, it's for consistency with the builtin module type):

from modulefinder import ModuleFinder

finder = ModuleFinder()
finder.run_script('test.py')

appdir = '/path/to/project'

modules = {name: mod for name, mod in finder.modules.items()
if mod.__file__ is not None
and mod.__file__.startswith(appdir)}

for name in modules.keys():
print(f"{name}")

How to I list imported modules with their version?

Because you have a list of strings of the module name, not the module itself. Try this:

for module_name in modules:
module = sys.modules[module_name]
print module_name, getattr(module, '__version__', 'unknown')

Note that not all modules follow the convention of storing the version information in __version__.

Get an ordered list of imported modules in python

Although it's not guaranteed that the dictionary is ordered in CPython-3.6 it is ordered in the current 3.6 versions of CPython.

So if you display sys.modules (a dictionary containing all loaded modules) it should be ordered by the "relative order of imports":

import sys

print(list(sys.modules))


Related Topics



Leave a reply



Submit