How to find list of modules which depend upon a specific module in python?

"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!

So - this answers "Find a list of modules which depend on a given one" - instead of how the question was initally phrased - which I answered above.

So - this answers "Find a list of modules which depend on a given one" - instead of how the question was initally phrased - which I answered above. As it turns out, this is a bit more complex: One have to find the dependency tree for all loaded modules, and invert it for each module, while preserving a loading order that would not break things. I had also posted this to brazillian's python wiki at: python.org.br/wiki/RecarregarModulos #!

/usr/bin/env python # coding: utf-8 # Author: João S. O. Bueno # Copyright (c) 2009 - Fundação CPqD # License: LGPL V3.0 from types import ModuleType, FunctionType, ClassType import sys def find_dependent_modules(): """gets a one level inversed module dependence tree""" tree = {} for module in sys.modules.values(): if module is None: continue treemodule = set() for attr_name in dir(module): attr = getattr(module, attr_name) if isinstance(attr, ModuleType): treemodule.

Add(attr) elif type(attr) in (FunctionType, ClassType): treemodule. Add(attr. __module__) return tree def get_reversed_first_level_tree(tree): """Creates a one level deep straight dependence tree""" new_tree = {} for module, dependencies in tree.items(): for dep_module in dependencies: if dep_module is module: continue if not dep_module in new_tree: new_treedep_module = set(module) else: new_treedep_module.

Add(module) return new_tree def find_dependants_recurse(key, rev_tree, previous=None): """Given a one-level dependance tree dictionary, recursively builds a non-repeating list of all dependant modules """ if previous is None: previous = set() if not key in rev_tree: return this_level_dependants = set(rev_treekey) next_level_dependants = set() for dependant in this_level_dependants: if dependant in previous: continue tmp_previous = previous.copy() tmp_previous. Add(dependant) next_level_dependants. Update( find_dependants_recurse(dependant, rev_tree, previous=tmp_previous, )) # ensures reloading order on the final list # by postponing the reload of modules in this level # that also appear later on the tree dependants = (list(this_level_dependants.

Difference( next_level_dependants)) + list(next_level_dependants)) return dependants def get_reversed_tree(): """ Yields a dictionary mapping all loaded modules to lists of the tree of modules that depend on it, in an order that can be used fore reloading """ tree = find_dependent_modules() rev_tree = get_reversed_first_level_tree(tree) compl_tree = {} for module, dependant_modules in rev_tree.items(): compl_treemodule = find_dependants_recurse(module, rev_tree) return compl_tree def reload_dependences(module): """ reloads given module and all modules that depend on it, directly and otherwise. """ tree = get_reversed_tree() reload(module) for dependant in treemodule: reload(dependant) This wokred nicely in all tests I made here - but I would not recoment abusing it. But for updating a running zope2 server after editing a few lines of code, I think I would use this myself.

I figured out that just looking for ModuleType attributes in the dir(module) is not good enough. A number of times the imports look like from xyz import abc. To handle this, one should also consider FunctionType and ClassType attributes in the dir(module) listing and for those ones, one should pickup their corresponding getattr(attr, 'module') and add them in dependencies – Shailesh Kumar Dec 17 '09 at 14:58 Indeed.

I will have to fix that - or remove the code from both places - it is so compelx now it "have" to work for whoever needs it. – jsbueno Dec 18 '09 at 12:09 FIXED the posted example. – jsbueno Dec 18 '09 at 12:19.

You might want to take a look at Ian Bicking's Paste reloader module, which does what you want already: pythonpaste.org/modules/reloader?highlig... It doesn't give you specifically a list of dependent files (which is only technically possible if the packager has been diligent and properly specified dependencies), but looking at the code will give you an accurate list of modified files for restarting the process.

Some introspection to the rescue: from types import ModuleType def find_modules(module, all_mods = None): if all_mods is None: all_mods = set(module) for item_name in dir(module): item = getattr(module, item_name) if isinstance(item, ModuleType) and not item in all_mods: all_mods. Add(item) find_modules(item, all_mods) return all_mods This gives you a set with all loaded modules - just call the function with your first module as a sole parameter. You can then iterate over the resulting set reloading it, as simply as: reload (m) for m in find_modules().

Just trying to understand this code. - start with a given module x - create an empty set of modules which have been imported by x - iterated over dir(x) to identify all items which happen to be modules - add them to the set of modules for x - do this recursively to find all dependencies for x I would probably need to start with this and get a reverse mapping done to identify set of all modules which depend on a specific module – Shailesh Kumar Dec 2 '09 at 7:09 Gah, why would you do this? Sys.

Modules already contains all loaded modules.... – Douglas Mayle Dec 2 '09 at 9:27 Wait - -do you need a "list of modules a module depends upon in python" or a list "of all modules which depend on a specific module"? I can come up with code for the later, not much more complex than this one - but the question is phrased for the former. – jsbueno Dec 2 '09 at 10:51 @jsbueno Yes you r right, I phrased the subject line incorrectly :( I have revised the subject line now.

I am looking for all modules whichc depend on a specific module. – Shailesh Kumar Dec 2 '09 at 15:57.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions