load_module for import entire package

Sergey sh0375 at gmail.com
Wed Dec 11 08:28:31 CET 2013

I need to import package and instantiate a class, defined in one of modules, located in package.

Package is located in folder "tmp". basedir - path to running python script.

I'm doing it so:

import imp

def load_package_strict(p_package_name, p_package_path):
    f, filename, description = imp.find_module(p_package_name, [p_package_path])
        result = imp.load_module(p_package_name, f, filename, description)
        if f: f.close
    return result

def get_obj():
  pkg = load_package_strict("tmp", basedir)
  from tmp import main
  return main.TTT()

It is working, but if package code changes on disc at runtime and I call get_obj again, it returns instance of class, loaded for the first time previously. 

How to replace line "from tmp import main" by getting properties of pkg?


More information about the Python-list mailing list