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
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.
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.
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.
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.
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
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
Update: This function requires IPython (pip install ipython
) as described here: Reloading submodules in IPython
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With