Re: trouble with reload

Discussion in 'Python' started by Terry Reedy, Aug 14, 2009.

  1. Terry Reedy

    Terry Reedy Guest

    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
    Terry Reedy, Aug 14, 2009
    #1
    1. Advertising

  2. 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
    Steven D'Aprano, Aug 14, 2009
    #2
    1. Advertising

  3. En Fri, 14 Aug 2009 05:34:52 -0300, Steven D'Aprano
    <> 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
    Gabriel Genellina, Aug 14, 2009
    #3
  4. 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.
    Colin J. Williams, Aug 14, 2009
    #4
  5. 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
    Steven D'Aprano, Aug 14, 2009
    #5
  6. 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.
    >
    >
    >
    Colin J. Williams, Aug 14, 2009
    #6
  7. 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
    Steven D'Aprano, Aug 14, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    9,083
  2. Joshua Beall
    Replies:
    26
    Views:
    219,881
    AndreiKaa
    Jun 29, 2007
  3. gen_tricomi
    Replies:
    2
    Views:
    722
    gen_tricomi
    May 23, 2006
  4. Josh Nikle
    Replies:
    5
    Views:
    348
    =?Utf-8?B?RGFsZQ==?=
    Oct 28, 2006
  5. Stefan Mueller
    Replies:
    4
    Views:
    278
    Stefan Mueller
    Nov 5, 2005
Loading...

Share This Page