Problems with importlib.reload

Skip to first unread message

Edward Elliott

Jul 30, 2023, 1:07:33 PM7/30/23
to brython

Has anyone else had problems with importlib.reload?  I see strange behavior, not sure if it's related to my environment or not.

Things I see:

1. reload () throws a "spec not found" error for custom modules.  foo is a custom module that was already loaded with no errors via 'import foo' at the top of the script.  Running importlib.reload (foo) causes the error message below.

2. reload () does strange things on console.  Some parts of module are reloaded, but other parts persist.  See example below.

3. To work around the issues above, I do a "manual reload" which seems to work ok.  Here's my code:

name = 'foo'

del globals () [name]

del sys.modules [name]

globals () [name] = __import__ (name)

Not ideal but it does reload the module from scratch. 

I don't know if this error is just my setup.  I'm using an older version (brython 3.8.9) inside a browser extension, so not a typical environment.  But issue #2 happens in latest brython console, so maybe it's not just me.

Thanks for any feedback.

Bython console example

>>> import math

>>> import importlib

>>> importlib.reload (math)

<module math at>

>>> math.x = 1

>>> math.sin = lambda x : x

>>> math.sin (1)

1   <-- altered value

>>> importlib.reload (math)

<module math at>

>>> math.sin (1)

0.8414709848078965  <-- good, sin has been reset to standard function

>>> math.x

1   <-- why is this still here??  math.x should throw an AttributeError after reload

Brython reload error

reloading module : fbvidext              (at brython.js:9027)    <--- my log message

spec not found for the module 'fbvidext'       (at brython.js:9027)

    at _b_.ModuleNotFoundError.$factory (eval at $make_exc (chrome-extension://vkdfjhgslojgsjldjnldngldngbldnbx/res/brython.js:7:266514), <anonymous>:1:211)

    at reload5 (eval at exec_module (chrome-extension://vkdfjhgslojgsjldjnldngldngbldnbx/res/brython.js:7:333557), <anonymous>:606:72)

    at reload_handler0 (eval at e.loop (chrome-extension://vkdfjhgslojgsjldjnldngldngbldnbx/res/brython.js:7:162411), <anonymous>:81:78)

    at chrome-extension://vkdfjhgslojgsjldjnldngldngbldnbx/res/brython.js:7:319267

Traceback (most recent call last):             (at brython.js:9028)

  File chrome-extension://vkdfjhgslojgsjldjnldngldngbldnbx/background.html/__main__ line 38, in reload_handler

            importlib.reload (fbvidext)

  File VFS.importlib/ line 174, in reload

    del _RELOADING[name]

ModuleNotFoundError: spec not found for the module 'fbvidext'          (at brython.js:9029)

Reply all
Reply to author
0 new messages