3

This question is similar to [Python: reload component Y imported with 'from X import Y'? ]. However, clearly reload doesnt work in python 3.

I initially had

from vb2GP import vb_Estep

However, due to a bug I modified vb_Estep. When I try to reload using importlib.reload(vb_Estep) I get the error:

  File "<ipython-input-61-72416bca3a93>", line 1, in <module>
    importlib.reload(vb_Estep)

  File "/Users/sachin/anaconda/lib/python3.5/importlib/__init__.py", line 139, in reload
    raise TypeError("reload() argument must be module")

TypeError: reload() argument must be module

I even tried importlib(vb2GP.vb_Estep) where I get the error NameError: name 'vb2GP' is not defined, which makes sense since I never imported vb2GP to start off with.

So the question is, how do you reload compnents in Python3 using importlib.

Community
  • 1
  • 1
sachinruk
  • 9,571
  • 12
  • 55
  • 86
  • Did you modify `vb_Estep` in place (if it is mutable) or did you rebind the name to some new object (which would be the only way to "modify" it if it's not mutable)? – Blckknght Mar 31 '16 at 03:16
  • I just rewrote a few lines. I'm pretty new to python so not entirely sure if that answers the question or not. – sachinruk Mar 31 '16 at 03:18

1 Answers1

3

The reload function you're using only works on modules, not directly on objects imported from them. To use it, you'll need to import vb2GP first, then call reload on the module object, then extract the reloaded vb_Estep value from the new version of the module.

import importlib

import vb2GP                      # import the old version of the module
vb2GP = importlib.reload(vb2GP)   # reload it
vb_Estep = vb2GP.vb_Estep         # get a reference to the value in the reloaded module

But that might not actually be necessary, depending on how exactly you "modified" the current vb_Estep value. If you have only rebound the name to a new value, you can just re-import the old one without doing any reloading at all (just use from vb2GP import vb_Estep again). It's only if you modified the value in place (e.g. by changing its contents or attributes without rebinding the variable name itself) that the reload code above will be needed.

Blckknght
  • 100,903
  • 11
  • 120
  • 169