update module on import to python interpreter

2019-07-03 19:14发布

问题:

In short

How to force python interpreter to load the most up-to-date code version of my module everytime I make some changes in the module code?

Or at least reload the last modified version by typing

>>> from myModule import *

into console, without necessity to restart whole python console and setup everything again and again anytime I make some changes? This is extremely unpleasant behavior for debugging.

--------- LONGER STORY -----------

I tried to delete the .pyc file, and import it again - but it has no effect. It does't even create .pyc file again - so I expect it completely ignore my "import" command if the module is already loaded.

this also does not help:

>>> mymodule.myfunc()    # the old version
>>> del myModule         # unload mymodle from python conole / interpeter
...                  # now I removed .pyc
...                  # now I make some modifications in mymodule.myfunc() code   
>>> mymodule.myfunc()    # module is unknonwn, ... OK
>>> import myModule      # try to load modified version
>>> mymodule.myfunc()    # stil the old version :(((((, How it can remember?

I have tried also Spyder where is this feature called "User Module Deleter (UMD)" http://pythonhosted.org/spyder/console.html#reloading-modules-the-user-module-deleter-umd which I thought should do exactly this, but it seem it doesn't (Yes, I checked that it is turned on).

Maybe I'm missing something - can somebody explain me how is it supposed to be used?

Is this somehow affected by the fact that the imported module is not in "Working directory" but in PYTHONPATH ?

回答1:

(Spyder dev here) I think at the moment you are not able to reload a module directly in the console (but we are considering to change this in the future).

The idea about UMD is that it will reload your modules but only if you run a file from the editor that imports them. It doesn't work if you want to reload them directly in the console.

Let's say you developed a module, then you are probably using it in a different script that (most likely) you'll be writing in our editor and send it to run to our console. UMD is a little bit of magic that reloads it for you when that happens.



回答2:

Maybe useful for others. In Spyder 3.0.0, the modules can be reloaded by,

Tools -> Update modules names list.

It worked for me.