Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reload a module after changing it?

Tags:

pycharm

Python Console with Python 3.4.2

I defined a function in a module which runs correctly in Python Console in PyCharm Community Edition 4.5.4:
ReloadTest.py:

def reloadtest(x):     print("Version A: {}".format(x)) 

Python Console:

Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:15:05) [MSC v.1600 32 bit (Intel)] on win32 >>> from ReloadTest import reloadtest >>> reloadtest(1) Version A: 1    

After I modified the function to "Version B", PyCharm can't find the change, and importlib.reload(ReloadTest) gives me error.
I must reload the Python Console or restart PyCharm every time I modify a module. What did I do wrong? What is the best way to handle this?

ReloadTest.py:

def reloadtest(x):     print("Version B: {}".format(x)) 

Python Console:

>>> reloadtest(1) Version A: 1 >>> from ReloadTest import reloadtest >>> reloadtest(1) Version A: 1 >>> import importlib >>> importlib.reload(ReloadTest) Traceback (most recent call last):   File "<input>", line 1, in <module> NameError: name 'ReloadTest' is not defined >>> from ReloadTest import reloadtest >>> reloadtest(1) Version A: 1 >>> import ReloadTest >>> reloadtest(1) Version A: 1 
like image 241
hxin Avatar asked Oct 24 '15 21:10

hxin


People also ask

How do you reload a module?

The reload() is a previously imported module. If you've altered the module source file using an outside editor and want to test the updated version without leaving the Python interpreter, this is helpful. The module object is the return value.

How do you refresh Python import?

The reload() - reloads a previously imported module or loaded module. This comes handy in a situation where you repeatedly run a test script during an interactive session, it always uses the first version of the modules we are developing, even we have mades changes to the code.

What is reload function in Python?

The function reload(moduleName) reloads a previously loaded module (assuming you loaded it with the syntax "import moduleName". It is intended for conversational use, where you have edited the source file for a module and want to test it without leaving Python and starting it again.

How do you reload a Spyder module?

Go to Tools -> Preferences -> Python Interpreter and you will find User Module Reloader. Just deselect the checkbox with text as Show reloaded module list. Show activity on this post. Click on Apply (Anwenden) and restart Spyder.


2 Answers

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 
like image 96
Eskapp Avatar answered Oct 07 '22 01:10

Eskapp


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

like image 36
Morris Franken Avatar answered Oct 06 '22 23:10

Morris Franken