0

I've found another post that seems to address the case for importing a single object from a given module, e.g.:

from module_abc import func_xyz

However, what's the proper way to reload all the objects if they were imported using * (with the assumption that this is done in a jupyter notebook if that matters)?:

from module_abc import *

From what I've found, the library to use is importlib. According to their docs, they say that

one way around this is to re-execute the from statement, another is to use import and qualified names (module.name) instead.

Tried both ways. The former way doesn't work if you're trying to reload the object dynamically in a jupyter notebook. The latter doesn't actually address this question directly.

nwly
  • 1,360
  • 4
  • 16
  • 30
  • The proper way is to avoid doing "star" imports in the first place. ;) BTW, when you do any kind of import, the whole module is imported, so doing something like `from module_abc import func_xyz` imports the whole of `module_abc`, but it only injects the name `func_xyz` from that module into your namespace. – PM 2Ring Oct 04 '17 at 20:08
  • 1
    Reloading is really hard to get right, and the built-in `reload` only covers the very simplest use cases. Your best bet is almost always to restart Python, even if it's inconvenient; anything less is liable to leave scattered references to old versions of module contents in tricky places. – user2357112 Oct 04 '17 at 20:12
  • I understand that it's bad form, etc., but sometimes when you're prototyping something speed is of the essence. I miss how with py2.x you could use reload(sys.modules['modulename']). – nwly Oct 04 '17 at 22:22
  • You can still do that with `importlib.reload`, but if you thought that would have solved your current problem on Python 2, you thought wrong. `reload` never handled this. – user2357112 Oct 05 '17 at 16:28
  • I don't understand. Calling `reload(sys.modules['modulename'])` followed by `from modulename import *` in Python 2 does exactly what I wanted to do in this question. – nwly Oct 05 '17 at 20:50

0 Answers0