New submission from John Lehmann:

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__"

----------
files: reloader.py
messages: 284982
nosy: brett.cannon, j1o1h1n, r.david.murray
priority: normal
severity: normal
status: open
title: importlib reload fails for module supplied as argument to command line
type: behavior
versions: Python 3.5
Added file: http://bugs.python.org/file46212/reloader.py

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue29206>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to