reloading the module imported as 'from ... import ...'

A

AlF

Hi,

what is the best way to reload the module imported using 'from ...
import ...'

Is following a way to do so?

Traceback (most recent call last):
<module 'email.charset' from '/usr/lib/python2.5/email/chars


Probably it works but I do not like it as I end up with two namespaces
for the symbol Charset: email.charset.Charset and Charset

Thx,
A.
 
A

AlF

Steven said:
Have you tried "from ... import ..." again?

I have not because of an assumption that "import" imports the module
just once. In fact this still works that way:

here is a terminal 1:

$ cat > a.py
a=1
$ cat > a.py
a=2
$


and terminal 2:

In spite of changing a.py in fly, the imported a is still 1
 
S

Steven D'Aprano

I have not because of an assumption that "import" imports the module
just once.

Ah, of course the cached module will still reflect the older version.
Sorry, I was thinking about solving a different problem:

- In module "main" call "from A import a"
- Some other part of your code modifies A.a
- You want to have the imported a be refreshed with the value of A.a


No, my suggestion won't help in this situation.

Instead, you can:

(1) Delete the module from sys.modules, forcing Python to re-read it from
disk:

import sys
del sys.modules['A']
from A import a

or

(2) Recognize that Python doesn't specifically support what you're trying
to do. reload() is a convenience function, and you probably should stick
to the "import A; A.a" form.
 
P

Piet van Oostrum

SD> Ah, of course the cached module will still reflect the older version.
SD> Sorry, I was thinking about solving a different problem:

If you do a reload in between the cached version is replaced by the new
version. However, objects from the old module that have references
before the reload will still lie around. This could cause very subtle
bugs.

Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39)
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
Type "help", "copyright", "credits" or "license" for more information.False
 

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,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top