How do I reload a module after changing it?

You can instruct Pycharm to automatically reload modules upon changing by adding the following lines to settings->Build,Excecution,Deployment->Console->Python Console in the Starting Script:

%load_ext autoreload
%autoreload 2

enter image description here

Update: This function requires IPython (pip install ipython) as described here: Reloading submodules in IPython


I took me some time to understand the previous answer... And also, that answer is not very practical if the chuck of code you need to run is in the middle of a script that you do not feel like modifying for running it once.

You can simply do:

import importlib
importlib.reload(my_module)
from my_module import my_function

Then, you can run your code with the updated version of the function.

Works with PyCharm Community Edition 2016.3.2

Edit w.r.t. first comment: This only works if you first imported the module itself (otherwise you get an error as said in the first comment).

import my_module
from my_module import my_function
# Now calls a first version of the function
# Then you change the function
import importlib
importlib.reload(my_module)
from my_module import my_function
# Now calls the new version of the function

Tags:

Pycharm