load_module for import entire package

S

Sergey

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
 
D

Dave Angel

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.
 
A

alex23

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()
 
S

Sergey

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top