I have a Python Module test, I want to edit test and run some functions in test at the interpreter. Will Python automatically see the differences in the functions that I edit in test without reimporting? What is the appropriate word for this action?
- What are the best practices for reimporting/reloading/redefining?
- How can this be done efficiently? (It seems inefficient to highlight text with a mouse and then copy and paste).
EDIT Nothing mentioned so far is working so I will post a few more details:
A class is in my module, called Test. So I used the statement from test import Test. Now when I try the command reload(test) the interpreter tells me reload is undefined. If I do import imp then imp.reload(test) the interpreter tells me that test is undefined. What is going wrong here?
I suspect you’re using Python 3, and all of the answerers are assuming you’re using Python 2.
In Python 3,
reloadwas moved frombuiltinstoimp.So, the answer is:
For future reference, if you’re using Python 3, it’s almost always worth mentioning it, unless you know for a fact that it’s not relevant, because many of the old-timers (read: the people you want answer from) will assume 2.7.
Meanwhile, back to your main question:
For simple cases,
reload()orimp.reload()are exactly what you want. (For super-simple cases, there’s nothing wrong with copy and paste…)But it’s very easy to confuse yourself when you have, e.g., some object
barof classfoo.Barand then reloadfoo. It also doesn’t play too well withfrom foo import *, which is often handy for interactive sessions.If you can start up a new interpreter session and import the module, that’s much cleaner. If you need a bunch of objects created to start playing with the module, you can temporarily add them as globals to the module, or create a wrapper module that defines them. Some IDEs can wrap all of this up so just hitting some accelerator key sequence causes it to restart the interpreter and rerun your
importand all your setup stuff.