load_module for import entire package

alex23 wuwei23 at gmail.com
Thu Dec 12 01:58:00 EST 2013


On 11/12/2013 5:28 PM, Sergey wrote:
> 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?

Your `load_package_strict` function loads the `tmp` module and binds it 
to the name `pkg`. You then use a regular import to load `tmp.main`, 
which is _cached_; all subsequent occurrences reuse the initially 
imported value.

This should work:

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



More information about the Python-list mailing list