Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #61529 > unrolled thread
| Started by | Sergey <sh0375@gmail.com> |
|---|---|
| First post | 2013-12-10 23:28 -0800 |
| Last post | 2013-12-12 23:09 -0800 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
load_module for import entire package Sergey <sh0375@gmail.com> - 2013-12-10 23:28 -0800
Re: load_module for import entire package Dave Angel <davea@davea.name> - 2013-12-11 10:05 -0500
Re: load_module for import entire package alex23 <wuwei23@gmail.com> - 2013-12-12 16:58 +1000
Re: load_module for import entire package Sergey <sh0375@gmail.com> - 2013-12-12 23:09 -0800
| From | Sergey <sh0375@gmail.com> |
|---|---|
| Date | 2013-12-10 23:28 -0800 |
| Subject | load_module for import entire package |
| Message-ID | <cbf4a2a0-82d9-4f4f-9b1c-c6baa36b1aee@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-12-11 10:05 -0500 |
| Message-ID | <mailman.3901.1386774277.18130.python-list@python.org> |
| In reply to | #61529 |
On Tue, 10 Dec 2013 23:28:31 -0800 (PST), Sergey <sh0375@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
[toc] | [prev] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2013-12-12 16:58 +1000 |
| Message-ID | <l8bmpr$8s1$1@dont-email.me> |
| In reply to | #61529 |
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()
[toc] | [prev] | [next] | [standalone]
| From | Sergey <sh0375@gmail.com> |
|---|---|
| Date | 2013-12-12 23:09 -0800 |
| Message-ID | <53059a9f-5057-4912-8743-3b37cc00a5a2@googlegroups.com> |
| In reply to | #61671 |
> 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web