简体   繁体   中英

Eclipse PyDev's Console cannot notice the change of imported file

I am new for both PyDev and Python, and Looking for help for following question. Suppose I have two files test1.py and test2.py, and their contents are as follows:

test1.py

from test2 import a
print a

test2.py

a=1

When I run the console, and use %run test1 to run the code test1.py, everything is fine, and I got output 1 . However, if I changed the value of a in the file test2.py (such as from 1 to 2), and then use %run test1 , I will still get 1 . I tried to use execfile('test2.py') to reload test2.py, which however doesn't work.

Every thing will be ok, if I use the terminal to run code, ie python test1.py , which will notice the change in test2.py whenever I run it. Only the console in eclipse is weird.

Note: The Operating System that I use is Windows 7.

Thanks for any help or attention in advance.

Actually, PyDev has a builtin solution for that too:

Enable User Module Deleter at preferences > pydev > interactive console > user module deleter and then, instead of execfile or %run, use runfile('file_to_run.py') -- note that if you have the editor opened in PyDev, you can use Ctrl+Alt+Enter to send that statement to the interactive console for you.

By doing so, runfile will remove all the modules previously imported and reimport your new modules...

Another option is really reloading manually through the reload(module) function.

I found a way to handle this. The key to do this is the command reload() . But not with current content, because it is a bad idea to import a variable from a module, ie from test2 import a .

So the file 'test1.py' should be changed.

test1.py

import test2
print test2.a

And after this, whenever I changed 'test2.py', I run the code reload(test2) , and then use %run test1 will give the correct answer of a .

Following link is similar question with more detail explanation for this stuff is given below. Python: reload component Y imported with 'from X import Y'?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM