Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: trouble with reload

Reply
Thread Tools

Re: trouble with reload

 
 
Terry Reedy
Guest
Posts: n/a
 
      08-14-2009
Dr. Phillip M. Feldman wrote:
> According to the Python documentation, 'reload' reloads a previously imported
> module (so that changes made via an external editor will be effective).
> But, when I try to use this command, I get the following error message:
>
> TypeError: reload() argument must be module
>
> Any suggestions will be appreciated.


Besides the other answers, do not use reload. It is removed in Py3
because it cannot be made to work as people reasonably expect.

tjr

 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      08-14-2009
On Fri, 14 Aug 2009 13:49:19 +0900, Terry Reedy wrote:

> Dr. Phillip M. Feldman wrote:
>> According to the Python documentation, 'reload' reloads a previously
>> imported module (so that changes made via an external editor will be
>> effective). But, when I try to use this command, I get the following
>> error message:
>>
>> TypeError: reload() argument must be module
>>
>> Any suggestions will be appreciated.

>
> Besides the other answers, do not use reload. It is removed in Py3
> because it cannot be made to work as people reasonably expect.


That's a damn shame, because it is very useful for interactive use once
you get it's quirks. Is it gone-gone or just removed from built-ins?

If the former, would the following be a reasonable replacement?

def reload(module):
if type(module) is not type(__builtins__):
raise TypeError("reload() argument must be module")
name = module.__name__
del globals()[name]
del sys.modules[name]
globals()[name] = __import__(name)


It seems to work for me, but I'm not sure if I've missed something.


--
Steven
 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      08-14-2009
En Fri, 14 Aug 2009 05:34:52 -0300, Steven D'Aprano
<(E-Mail Removed)> escribió:
> On Fri, 14 Aug 2009 13:49:19 +0900, Terry Reedy wrote:


>> Besides the other answers, do not use reload. It is removed in Py3
>> because it cannot be made to work as people reasonably expect.

>
> That's a damn shame, because it is very useful for interactive use once
> you get it's quirks. Is it gone-gone or just removed from built-ins?


Just relocated:

p3> import imp
p3> imp.reload
<built-in function reload>

--
Gabriel Genellina

 
Reply With Quote
 
Colin J. Williams
Guest
Posts: n/a
 
      08-14-2009
Terry Reedy wrote:
> Dr. Phillip M. Feldman wrote:
>> According to the Python documentation, 'reload' reloads a previously
>> imported
>> module (so that changes made via an external editor will be
>> effective). But, when I try to use this command, I get the following
>> error message:
>>
>> TypeError: reload() argument must be module
>>
>> Any suggestions will be appreciated.

>
> Besides the other answers, do not use reload. It is removed in Py3
> because it cannot be made to work as people reasonably expect.
>
> tjr
>

It's typically a user module that needs to be reloaded.

It seems that del sys.modules['moduleName'] has no effect.

Is there some other way of ensuring that any import goes to moduleName.py,
instead of moduleName.pyc?

Colin W.
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      08-14-2009
On Fri, 14 Aug 2009 09:23:17 -0400, Colin J. Williams wrote:

> It's typically a user module that needs to be reloaded.


What's a user module?


> It seems that del sys.modules['moduleName'] has no effect.


sys.modules is just a dictionary, I find it hard to believe that deleting
from it has no effect. It works for me:

>>> import sys
>>> import math
>>> 'math' in sys.modules

True
>>> del sys.modules['math']
>>> 'math' in sys.modules

False

What behaviour do you get?


Of course deleting the math module from the cache doesn't do anything to
the math module in your namespace:

>>> math

<module 'math' from '/usr/lib/python2.5/lib-dynload/mathmodule.so'>
>>> del math
>>> math

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'math' is not defined

Of course deleting the module (or reloading it) doesn't have any effect
on any objects you already have:


>>> import math
>>> func = math.sin
>>> del sys.modules['math']
>>> del math
>>> math.sin(1.2)

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'math' is not defined
>>> func(1.2)

0.93203908596722629




> Is there some other way of ensuring that any import goes to
> moduleName.py, instead of moduleName.pyc?


Delete moduleName.pyc.

Make sure the .pyc file doesn't exist in the first place.

Make sure the last modification date of the .py file is newer than the
modification date of the .pyc file.



--
Steven
 
Reply With Quote
 
Colin J. Williams
Guest
Posts: n/a
 
      08-14-2009
Steven D'Aprano wrote:
> On Fri, 14 Aug 2009 09:23:17 -0400, Colin J. Williams wrote:
>
>> It's typically a user module that needs to be reloaded.

>
> What's a user module?

A module written by a user, as distinguished from a libary
>
>
>> It seems that del sys.modules['moduleName'] has no effect.

>
> sys.modules is just a dictionary, I find it hard to believe that deleting
> from it has no effect. It works for me:
>
>>>> import sys
>>>> import math
>>>> 'math' in sys.modules

> True
>>>> del sys.modules['math']
>>>> 'math' in sys.modules

> False
>
> What behaviour do you get?
>
>
> Of course deleting the math module from the cache doesn't do anything to
> the math module in your namespace:
>
>>>> math

> <module 'math' from '/usr/lib/python2.5/lib-dynload/mathmodule.so'>
>>>> del math
>>>> math

> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> NameError: name 'math' is not defined
>
> Of course deleting the module (or reloading it) doesn't have any effect
> on any objects you already have:
>
>
>>>> import math
>>>> func = math.sin
>>>> del sys.modules['math']
>>>> del math
>>>> math.sin(1.2)

> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> NameError: name 'math' is not defined
>>>> func(1.2)

> 0.93203908596722629
>
>
>
>
>> Is there some other way of ensuring that any import goes to
>> moduleName.py, instead of moduleName.pyc?

>
> Delete moduleName.pyc.
>
> Make sure the .pyc file doesn't exist in the first place.
>
> Make sure the last modification date of the .py file is newer than the
> modification date of the .pyc file.


That's easier said than done, when one is working with an IDE. The cached
..pyc file might be different from that in the file.

Colin W.
>
>
>

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      08-14-2009
On Fri, 14 Aug 2009 13:14:16 -0400, Colin J. Williams wrote:

> Steven D'Aprano wrote:
>> On Fri, 14 Aug 2009 09:23:17 -0400, Colin J. Williams wrote:
>>
>>> It's typically a user module that needs to be reloaded.

>>
>> What's a user module?

> A module written by a user, as distinguished from a libary


You mean an end-user of the application? Or you mean the application
programmer?

I'm not sure there is any difference between "user modules" and "library
modules" apart from what they do.

Perhaps it would help if you could explain the circumstances of when you
would reload a module. During application development, for an incremental
edit/reload/test cycle? Or while the completed application is being
executed?


>>> Is there some other way of ensuring that any import goes to
>>> moduleName.py, instead of moduleName.pyc?

>>
>> Delete moduleName.pyc.
>>
>> Make sure the .pyc file doesn't exist in the first place.
>>
>> Make sure the last modification date of the .py file is newer than the
>> modification date of the .pyc file.

>
> That's easier said than done, when one is working with an IDE. The
> cached .pyc file might be different from that in the file.


If the IDE is getting in your way, then don't use it.

Or if you tell us what IDE you're using, and precisely what it is doing,
somebody can tell you how to defeat the IDE's cache.

(That won't be me -- I don't use IDEs.)

Or perhaps you're entire approach is wrong, and you shouldn't be using
reload() at all. Don't know.



--
Steven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using refresh to reload page, problem is it loads header, footer andeverything, can I avoind those to reload? VT HTML 8 10-13-2010 11:09 PM
JavaScript refresh: <a href="#" onClick="window.location.reload( true );">Reload</a> Joshua Beall HTML 26 06-29-2007 06:31 PM
problem with reload(sys) (doing reload on the sys module) gen_tricomi Python 2 05-23-2006 10:03 AM
Is there an SNMP equivalent to "Reload in [x]" / "Reload cancel" commands? steve.chambers@gmail.com Cisco 5 05-20-2006 04:06 AM
How to get and cancel the reload event (F5, Reload nutton, ...) within a browser Stefan Mueller Javascript 4 11-05-2005 06:09 PM



Advertisments