classification
Title: importlib reload fails for module supplied as argument to command line
Type: Stage:
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, j1o1h1n, r.david.murray
Priority: normal Keywords:

Created on 2017-01-07 12:02 by j1o1h1n, last changed 2017-01-08 11:29 by j1o1h1n. This issue is now closed.

Files
File name Uploaded Description Edit
reloader.py j1o1h1n, 2017-01-07 12:02
Messages (4)
msg284910 - (view) Author: John Lehmann (j1o1h1n) * Date: 2017-01-07 12:02
Modules that have been loaded as an argument to the command line cannot be reloaded using importlib.reload.

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'
msg284913 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-01-07 12:19
I don't think reloading __main__ is something we have any desire to support, but there may be something in this error sequence worth thinking about anyway...the error message isn't very clear as to what went wrong, even with the traceback.

What is your use case for reloading __main__?
msg284927 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2017-01-07 18:22
The error actually makes sense when you think about the fact that the module is being passed in by filename. That means Python has no clue what values to put in for the spec because Python literally exec()'s the file contents and stuffs it into a new module name __main__. There's no fancy attempt to construct a proper module from scratch like if you had used -m which does fill in the spec. And since importlib.reload() relies entirely on the module's spec to handle reloads it simply can't handle the `__spec__ == None` case that this is triggering.

IOW use `python3 -m reloader` and it will probably work. Closing this as "not a bug".
msg284979 - (view) Author: John Lehmann (j1o1h1n) * Date: 2017-01-08 11:29
You get a slightly different error using "-m".

I'll raise a new defect - with some background that may make the bug seem more reasonable.
History
Date User Action Args
2017-01-08 11:29:23j1o1h1nsetmessages: + msg284979
2017-01-07 18:22:09brett.cannonsetstatus: open -> closed

nosy: + brett.cannon
messages: + msg284927

resolution: not a bug
2017-01-07 12:19:35r.david.murraysetnosy: + r.david.murray
messages: + msg284913
2017-01-07 12:02:13j1o1h1ncreate