1

While working on a module I use the repl (python console) to test the functionality.

The problem I'm trying to solve is easily reloading the module and reimporting the module objects after a modification.

An example shows the problem.

In a console I start testing a function in package a.b.c

>>> from a.b.c import *
>>> myFunction(1)
wrong answer

I go back to the text editor and fix myFunction Now I want to re test it (I don't want to kill the repl and restart it, since I might have some test variables that I want to reuse)

So I have to do something like this:

>>> import a.b.c
>>> from importlib import reload
>>> reload(a.b.c)
>>> from a.b.c import *
>>> myFunction(1)
hopefully the right answer

I would like to write a function that would do the reload and reimport * in one go.

Ideally I would like to replace the previous session with something like

>>> myTestReload(a.b.c)
>>> myFunction(1)
hopefully the right answer

In the myTestReload() function I can use reload(a.b.c) to reload the modified module but I didn't find a way to do the equivalent of from a.b.c import *

raduw
  • 1,008
  • 1
  • 9
  • 19
  • 2
    Possible duplicate of [Reloading submodules in IPython](https://stackoverflow.com/questions/5364050/reloading-submodules-in-ipython) – rst-2cv Jun 13 '18 at 12:02
  • Possible duplicate of [How do I unload (reload) a Python module?](https://stackoverflow.com/questions/437589/how-do-i-unload-reload-a-python-module) – Avezan Jun 13 '18 at 12:06
  • Thank you @Avezan , I do know about reloading a module, that is what I'm doing in the example. What I'm trying to achieve is to do it in just one go... call one function that does the reload and then reimports all the symbols from a module. I know how to realod the module but I didn't figure out how to programmatically re import * from the reloaded module. – raduw Jun 13 '18 at 12:12
  • @raduw: My bad I think you can give this one a try, posting. – Avezan Jun 13 '18 at 12:24

1 Answers1

2

This will reset globals here is an example with itertools.

import itertools
from itertools import *
from importlib import reload

itools = reload(itertools)

for k, v in itools.__dict__.items():
    if k in globals():
            globals()[k] = v
Avezan
  • 673
  • 4
  • 9
  • thanks, that is exactly the idea I was looking for, I'm trying your suggestion but It doesn't re import the items (I'm trying to figure out exactly why) and will post back – raduw Jun 13 '18 at 12:40
  • See if there is no lagging *.pyc remaining. It sometimes is problem. – Avezan Jun 13 '18 at 12:41
  • Further you can try with `getattr`, `inspect` etc. – Avezan Jun 13 '18 at 12:46
  • 1
    The code as written above does the job, if however it is placed in a function within a helper module then globals() returns the module dictionary (https://docs.python.org/3/library/functions.html?#globals) and therefore it will not add the members to the repl globals. Any way, thank you @Avezan for your insights. – raduw Jun 13 '18 at 14:23