8
votes

PyCharm runs an interactive Python console (IPython in my case), but when I make changes in the code, PyCharm doesn't reimport the modules I've been editing, so the console runs the old code.

More so, if I have an old package installed via Run setup.py Task, Python imports the old one after import mymodule as mm in the console.

One workaround is to edit the code in a file and rerun it without the interactive console, but that's not a very elegant solution.

How can I keep the interactive console up-to date and update modules on-the-fly?

2
I don't know if this answers your question, but in the python console you can type reload(module), being module a module object. I find it quite handy. - bgusach
I'd add that for Python 3, it's: import imp; imp.reload(module) - Anton Tarasenko
So, did it solve the problem? - bgusach
@Anton note that as of 2020-09-18 in pyton3.8 the package imp is deprecated in favour of importlib see Chaffelson answer below - loved.by.Jesus

2 Answers

5
votes

I'm using Python 3.4.3 in Pycharm 4.0.6 currently, and building on Anton's answer above, I add the following to my Starting script:

from importlib import reload

then you can simply use 'reload(my_module)' as you please - however this would be manual for each reload and I'm not aware of a fully automated solution if that's what you're really after.

1
votes

IPython has a very nice function that recursively reloads all modules. It is called dreload, from deep reload. You just have to manually run in the console:

dreload(myCoolModule)

It is manual, but it doesn't bothers me. If I remember well, it doesn't work if you remove the namespace importing like: from myCoolModule import *