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?
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?
Python Built-in Modulesprint() and input() for I/O, Number conversion functions such as int(), float(), complex(), Data type conversions such as list(), tuple(), set(), etc.
What are modules in Python? Modules refer to a file containing Python statements and definitions. A file containing Python code, for example: example.py , is called a module, and its module name would be example . We use modules to break down large programs into small manageable and organized files.
I suspect you're using Python 3, and all of the answerers are assuming you're using Python 2.
In Python 3, reload
was moved from builtins
to imp
.
So, the answer is:
imp.reload(test)
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:
What are the best practices for reimporting/reloading/redefining?
For simple cases, reload()
or imp.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 bar
of class foo.Bar
and then reload foo
. It also doesn't play too well with from 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 import
and all your setup stuff.
Will Python automatically see the differences in the functions that I edit in test without reimporting?
No.
What is the appropriate word for this action?
See the builtin reload(module).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With