Re: load_module for import entire package
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(1,1)) + '_' + 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: load_module for import entire package
On Tue, 10 Dec 2013 23:28:31 -0800 (PST), Sergey sh0...@gmail.com 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. That's how import works. Once something has been imported, the module information is cached. There are three ways to defeat that, but they're all risky. How to replace line from tmp import main by getting properties of pkg? No clue what you mean by that. -- DaveA -- https://mail.python.org/mailman/listinfo/python-list
Re: load_module for import entire package
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() -- https://mail.python.org/mailman/listinfo/python-list
load_module for import entire package
Hi, 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]) try: result = imp.load_module(p_package_name, f, filename, description) finally: 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? Regards, Sergey -- https://mail.python.org/mailman/listinfo/python-list