reload fails if module not in sys.path

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lonnie Princehouse

    #1

    reload fails if module not in sys.path

    So, it turns out that reload() fails if the module being reloaded isn't
    in sys.path.

    Maybe it could fall back to module.__file__ if the module isn't found
    in sys.path??
    .... or reload could just take an optional path parameter...

    Or perhaps I'm the only one who thinks this is silly:
    [color=blue][color=green][color=darkred]
    >>> my_module = imp.load_module (module_name, *imp.find_modul e(module_name,p ath))
    >>> reload(my_modul e)[/color][/color][/color]
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    ImportError: No module named whatever


    I guess I could just deal with this by fiddling with sys.path or using
    imp.load_module again, but.. um.. I like to complain. ;-)

    The context here is that I'm loading user-defined modules as plugins,
    and I don't want to keep the plugin directory in sys.path because of
    potential module name conflicts.

  • Steve Holden

    #2
    Re: reload fails if module not in sys.path

    Lonnie Princehouse wrote:[color=blue]
    > So, it turns out that reload() fails if the module being reloaded isn't
    > in sys.path.
    >
    > Maybe it could fall back to module.__file__ if the module isn't found
    > in sys.path??
    > ... or reload could just take an optional path parameter...
    >
    > Or perhaps I'm the only one who thinks this is silly:
    >
    >[color=green][color=darkred]
    >>>>my_module = imp.load_module (module_name, *imp.find_modul e(module_name,p ath))
    >>>>reload(my_m odule)[/color][/color]
    >
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > ImportError: No module named whatever
    >[/color]
    You appear to have failed to load some module called "whatever", which I
    presume is the value of module_name?[color=blue]
    >
    > I guess I could just deal with this by fiddling with sys.path or using
    > imp.load_module again, but.. um.. I like to complain. ;-)
    >[/color]
    That's OK, but you may find fiddling with sys.path is more productive :-)
    [color=blue]
    > The context here is that I'm loading user-defined modules as plugins,
    > and I don't want to keep the plugin directory in sys.path because of
    > potential module name conflicts.
    >[/color]
    Hmm. I know that if your module isn't in sys.modules your reload will fail:
    [color=blue][color=green][color=darkred]
    >>> import trPyCard # picking a moduule at random
    >>> import sys
    >>> del sys.modules['trPyCard']
    >>> reload(trPyCard )[/color][/color][/color]
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    ImportError: reload(): module trPyCard not in sys.modules[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Apart from that, it would seem sensible not to try and use reload if you
    haven't used a kosher import mechanism - there are many caveats on the
    reload() documentation, and the mechanisms it uses aren't spelled out
    anywhere but in the interpreter source.

    It would seem easier just to ensure that the plugins directory(ies)
    appear first on sys.path and use __import__(). imp is high magic.

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC www.holdenweb.com
    PyCon TX 2006 www.python.org/pycon/

    Comment

    • Fredrik Lundh

      #3
      Re: reload fails if module not in sys.path

      Lonnie Princehouse wrote:
      [color=blue]
      > Maybe it could fall back to module.__file__ if the module isn't found
      > in sys.path??
      > ... or reload could just take an optional path parameter...
      >
      > Or perhaps I'm the only one who thinks this is silly:
      >[color=green][color=darkred]
      > >>> my_module = imp.load_module (module_name, *imp.find_modul e(module_name,p ath))
      > >>> reload(my_modul e)[/color][/color]
      > Traceback (most recent call last):
      > File "<stdin>", line 1, in ?
      > ImportError: No module named whatever[/color]

      load_module doesn't do everything that import does.
      [color=blue]
      > I guess I could just deal with this by fiddling with sys.path or using
      > imp.load_module again, but.. um.. I like to complain. ;-)
      >
      > The context here is that I'm loading user-defined modules as plugins,
      > and I don't want to keep the plugin directory in sys.path because of
      > potential module name conflicts.[/color]

      so add the plugin-directory to the front of sys.path temporarily,
      and remove it after you've imported the plugins. (this also allows
      the plugin writers to split their plugins over multiple modules,
      something that can often be quite nice)

      </F>



      Comment

      • Lonnie Princehouse

        #4
        Re: reload fails if module not in sys.path

        It's not just load_module. Reload fails on modules imported normally
        if their paths are no longer in sys.path.

        Easy to reproduce example:

        bash$ mkdir module_dir
        bash$ touch module_dir/plugin.py
        bash$ python
        Python 2.4.1 (#1, Sep 25 2005, 15:12:45)
        [GCC 3.4.3 20041125 (Gentoo 3.4.3-r1, ssp-3.4.3-0, pie-8.7.7)] on
        linux2
        Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
        >>> import sys
        >>> sys.path.append ('module_dir')
        >>> import plugin
        >>> sys.path.pop()[/color][/color][/color]
        'module_dir'[color=blue][color=green][color=darkred]
        >>> reload(plugin) # plugin was imported correctly, but it can't be reloaded:[/color][/color][/color]
        Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        ImportError: No module named plugin[color=blue][color=green][color=darkred]
        >>>
        >>> sys.modules['plugin'] # ... although it is sys.modules:[/color][/color][/color]
        <module 'plugin' from 'module_dir/plugin.py'>

        Comment

        • Lonnie Princehouse

          #5
          Re: reload fails if module not in sys.path

          > That's OK, but you may find fiddling with sys.path is more productive :-)

          Yeah, that's what I'm doing and it works just fine. When I stumbled
          over this behavior yesterday it seemed (and still does) like a
          low-priority bug in reload. I was hoping a guru would reply with
          something like, "Of course that's how it is. If reload() tried to use
          the __file__ attribute, a universe-ending paradox would ensue
          because..."

          Feh.

          Comment

          Working...