This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author j1o1h1n
Recipients brett.cannon, j1o1h1n, r.david.murray
Date 2017-01-08.11:43:51
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1483875831.65.0.670865149709.issue29206@psf.upfronthosting.co.za>
In-reply-to
Content
In testing the py3 port for the web framework web.py, I found a limitation of importlib.reload.

A module that was loaded via the command line cannot be reloaded with importlib.reload.

The basic mode of operation for web.py is to create a web application as a single module, say, "app.py" and run this with "python app.py".

When in development mode, on each page view, the modification time for each file backing each module is checked for changes.  If the file has changed, the file is reloaded.

This allows for an iterative development mode familiar to web developers since the glory days of writing VB pages in ASP.

The problem occurs when the file is loaded directly, or with "-m".

For example with the attached file:

  $ python reloader.py
  Traceback (most recent call last):
    File "reloader.py", line 31, in <module>
      reload_module("__main__")
    File "reloader.py", line 28, in reload_module
      importlib.reload(module)
    File "/usr/local/var/pyenv/versions/3.5.2/lib/python3.5/importlib/__init__.py", line 166, in reload
      _bootstrap._exec(spec, module)
    File "<frozen importlib._bootstrap>", line 607, in _exec
  AttributeError: 'NoneType' object has no attribute 'name'

And with -m:

  $ python -m reloader
  Traceback (most recent call last):
    File "/usr/local/var/pyenv/versions/3.5.2/lib/python3.5/runpy.py", line 184, in _run_module_as_main
      "__main__", mod_spec)
    File "/usr/local/var/pyenv/versions/3.5.2/lib/python3.5/runpy.py", line 85, in _run_code
      exec(code, run_globals)
    File "reloader.py", line 31, in <module>
      reload_module("__main__")
    File "reloader.py", line 28, in reload_module
      importlib.reload(module)
    File "/usr/local/var/pyenv/versions/3.5.2/lib/python3.5/importlib/__init__.py", line 147, in reload
      raise ImportError(msg.format(name), name=name)
  ImportError: module reloader not in sys.modules

Note that there are two different error messages, neither of which is great.

There are workarounds, but given that this works in previous version of python, it might be better to fix it.  Or to have an explicit error message that describes the problem more precisely:

  "Cannot reload __main__"
History
Date User Action Args
2017-01-08 11:43:51j1o1h1nsetrecipients: + j1o1h1n, brett.cannon, r.david.murray
2017-01-08 11:43:51j1o1h1nsetmessageid: <1483875831.65.0.670865149709.issue29206@psf.upfronthosting.co.za>
2017-01-08 11:43:51j1o1h1nlinkissue29206 messages
2017-01-08 11:43:51j1o1h1ncreate