Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
556 views
in Technique[技术] by (71.8m points)

rebuild - update module on import to python interpreter

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

(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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...