How to reboot a module after changing it?

Python Console with Python 3.4.2

I defined a function in a module that works correctly in the 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 changed the function to "Version B", PyCharm cannot find the changes, and importlib.reload(ReloadTest) gives me an error.
I have to restart the Python Console or restart PyCharm every time I change the module. What have I done 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 
+5
source share
3 answers

Get it to work!
Instead of from Module import function I have to import the entire module Import module, and then call Module.function() . It's because

 from ReloadTest import reloadtest 

and

 importlib.reload(ReloadTest) 

can't go together.

 >>> import ReloadTest >>> ReloadTest.reloadtest(1) Version A: 1 

After making changes:

 >>> importlib.reload(ReloadTest) <module 'ReloadTest' from 'C:\\...\\ReloadTest.py'> >>> ReloadTest.reloadtest(2) Version B: 2 
+2
source

I took some time to understand the previous answer ... And also, this answer is not very practical if the code cartridge you need to run is in the middle of a script that you do not like to modify to run 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 an updated version of the function.

Works with PyCharm Community Edition 2016.3.2

Edit wrt first comment: This only works if you first imported the module itself (otherwise you will 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 
+2
source

Since this question is specifically asked about PyCharm, please see the answer on the PyCharm support site . This answer is almost identical to @wokbot, the difference is that PyCharm eliminates the need for import importlib . It also uses the from ... import ... trick after import ... to simplify text input if you plan to use reloadtest often.

 In[2]: import ReloadTest In[3]: from ReloadTest import reloadtest In[4]: reloadtest(1) Out[4]: Version A: 1 

... make changes

 In[5]: reload(ReloadTest) Out[5]: <module 'ReloadTest' from 'C:\\...\\ReloadTest.py'> In[6]: from ReloadTest import reloadtest In[7] reloadtest(2) Out[7]Version B: 2 
0
source

Source: https://habr.com/ru/post/1234446/


All Articles