4

I have two python files, one is calling (importing) the other. But when I make a change in the module which is called, change is not applied unless I quit from Python and re-open it. Then I can see that changes are working. Here is a short example:

mainfile.py:

from otherfile import myfunc
a=5
myfunc(a)

otherfile.py:

def myfunc (x):
    print(x+3)

when I run mainfile.py, it prints 8 as expected. But when I change the last line in otherfile.py to print(x+4), and save it, running mainfile.py still prints 8! After quitting from Python and re-enter it, now it prints 9.

I googled this issue but couldn't find anything. Some people talks about "init", I have no idea what it is. I am very new to Python, so I appreciate if you explain this in a very basic way.

Thanks!

Note: I am using Python 3.6.1 on Anaconda 4.4.0 with Spyder 3.1.4

JohnPython
  • 637
  • 1
  • 7
  • 10
  • module-level python code is only re-executed after first-time use if the module is detected as having changed (touching the module - i.e. changing its mod. time - appears to be enough for this to work). Indeed, this makes a lot of sense – Fady Saad Sep 30 '17 at 23:38
  • I've found Spyder to do fresh imports every time you run you code, by default. Perhaps delete `.pyc` files after the change. – roganjosh Sep 30 '17 at 23:40
  • a better way is suggested here ,https://stackoverflow.com/questions/684171/how-to-re-import-an-updated-package-while-in-python-interpreter – benaou mouad Oct 10 '19 at 15:32

0 Answers0