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

3
  • 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 Commented Sep 30, 2017 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. Commented Sep 30, 2017 at 23:40
  • a better way is suggested here ,stackoverflow.com/questions/684171/… Commented Oct 10, 2019 at 15:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.