Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
reloading the module imported as 'from ... import ...'
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
AlF  
View profile  
 More options Aug 9 2009, 11:43 pm
Newsgroups: comp.lang.python
From: AlF <spamgrinder.tryla...@ggmail.com>
Date: Sun, 09 Aug 2009 20:43:41 -0700
Local: Sun, Aug 9 2009 11:43 pm
Subject: reloading the module imported as 'from ... import ...'
Hi,

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

Is following a way to do so?

 >>> from email.charset import Charset
 >>> reload(email.charset)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
NameError: name 'email' is not defined
 >>>
 >>>
 >>> import email.charset
 >>> reload(email.charset)
<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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Aug 10 2009, 1:32 am
Newsgroups: comp.lang.python
From: Steven D'Aprano <ste...@REMOVE.THIS.cybersource.com.au>
Date: 10 Aug 2009 05:32:36 GMT
Local: Mon, Aug 10 2009 1:32 am
Subject: Re: reloading the module imported as 'from ... import ...'

On Sun, 09 Aug 2009 20:43:41 -0700, AlF wrote:
> Hi,

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

Have you tried "from ... import ..." again?

--
Steven


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
AlF  
View profile  
 More options Aug 10 2009, 1:48 am
Newsgroups: comp.lang.python
From: AlF <spamgrinder.tryla...@ggmail.com>
Date: Sun, 09 Aug 2009 22:48:31 -0700
Local: Mon, Aug 10 2009 1:48 am
Subject: Re: reloading the module imported as 'from ... import ...'

Steven D'Aprano wrote:
> On Sun, 09 Aug 2009 20:43:41 -0700, AlF wrote:

>> Hi,

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

> 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:

 >>> from a import a
 >>> a
1
 >>> a
1
 >>> from a import a
 >>> a
1
 >>>

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Aug 10 2009, 2:21 am
Newsgroups: comp.lang.python
From: Steven D'Aprano <ste...@REMOVE.THIS.cybersource.com.au>
Date: 10 Aug 2009 06:21:36 GMT
Local: Mon, Aug 10 2009 2:21 am
Subject: Re: reloading the module imported as 'from ... import ...'

On Sun, 09 Aug 2009 22:48:31 -0700, AlF wrote:
> Steven D'Aprano wrote:
>> On Sun, 09 Aug 2009 20:43:41 -0700, AlF wrote:

>>> Hi,

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

>> Have you tried "from ... import ..." again?

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

--
Steven


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Piet van Oostrum  
View profile  
 More options Aug 10 2009, 11:00 am
Newsgroups: comp.lang.python
From: Piet van Oostrum <p...@cs.uu.nl>
Date: Mon, 10 Aug 2009 17:00:34 +0200
Local: Mon, Aug 10 2009 11:00 am
Subject: Re: reloading the module imported as 'from ... import ...'

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.

>>> import testmod
>>> from testmod import TestClass
>>> a = TestClass()
>>> b = TestClass()
>>> a.__class__ is b.__class__
True
>>> reload(testmod)

<module 'testmod' from 'testmod.pyc'>
>>> c = TestClass()
>>> c.__class__ is a.__class__
True
>>> from testmod import TestClass
>>> d = TestClass()
>>> d.__class__ is a.__class__

False

--
Piet van Oostrum <p...@cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »