load_module for import entire package

Sergey sh0375 at gmail.com
Fri Dec 13 08:09:06 CET 2013


> This should work:
>      def get_obj():
>         tmp = load_package_strict("tmp", basedir)
>         return tmp.main.TTT()
Thank you:)
I can only mention that it is working only if __init__.py of pkg contains line: import main

To avoid modules caching I copy package to the new folder with another name and import it using this new name.

    tmp_pkg = "t" + str(randint(10000,100000000)) + '_' + time.strftime("%Y%m%d%H%M%S", time.localtime())
    shutil.copytree(pkg_dir, basedir + '/x/' + dir_name + '/' + tmp_pkg)

    pkg = load_package_strict(tmp_pkg, basedir + '/x/' + dir_name)
    result = pkg.main.TTT()
What is the risk of this method? What are 3 methods of doing the same?

--
Sergey




More information about the Python-list mailing list