Python dependency analyzer library

I need to find the dependencies for each of my Python package submodules at runtime so that I can initialize them in the correct order (see current solution < EDIT: href = "http://openblox.hg.sourceforge.net/hgweb/openblox/openblox/file/c4ee2c09fe03/src/obengine/depman.py" rel = "nofollow"> here , which does not work well), so first I used the standard Python module modulefinder , but it was so too slow (~ 1-2 seconds per module).

My next choice was to analyze all the global values ​​of each module and find from those globals on which each submodule depends. (This is my current EDIT solution : I now have a better solution - see My answer). This algorithm is much faster than modulefinder (200 ms per module is required), but it works only for relative imports, and not for a fully qualified import style, which is unacceptable.

So I need either:

  • A quick alternative to modulefinder
  • Alternative algorithm

NOTE. I call my dependency analyzer at the beginning of each module, for example:

# File my_package/module3.py

import my_package.module1 # Some misc. module
import my_package.module2 # Some other misc. module
import my_package.dependency_analyzer

my_package.dependency_analyzer.gendeps()

(Just in case, this will help you.)

Thank!

EDIT: I have a solution now - see my answer.

+5
1

, :)

, dependency_analyzer, :

import sys
from sys import _getframe as getframe
import atexit

examined_modules = []

def gendeps():
    """Adds the calling module to the initialization queue."""
    # Get the calling module name, and add it to the intialization queue
    calling_module_name = getframe(1).f_globals['__name__']
    examined_modules.append(calling_module_name)

def init():
    """Initializes all examined modules in the correct order."""

    for module in examined_modules:
        module = sys.modules[module]
        if hasattr(module, 'init'):
            module.init()
        if hasattr(module, 'deinit'):
            # So modules get de-initialized in the correct order,
            # as well
            atexit.register(module.deinit)

, ( - ), gendeps. , , , gendeps. , gendeps , , .

+2
source

All Articles