5

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?

  1. What are the best practices for reimporting/reloading/redefining?
  2. 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?

CodeKingPlusPlus
  • 15,383
  • 51
  • 135
  • 216

4 Answers4

4

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.

abarnert
  • 354,177
  • 51
  • 601
  • 671
2

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).

Dolph
  • 49,714
  • 13
  • 63
  • 88
2

Well, for starters, you should absolutely not have a module named test because Python already has one in its standard library.

test is undefined because you imported Test, not test. You need to import test as well.

But note that due to the way imp.reload() works, reloading test will not update Test; that will still refer to the old copy of the class. You'd have to from test import Test again, as well.

The full sequence of events:

>>> import imp
>>> import test
>>> from test import Test
>>> Test().whatever()
# edit your code
>>> imp.reload(test)
>>> from test import Test  # or: Test = test.Test
>>> Test().whatever()

The moral of this story is that reload sucks and you should avoid it at all costs. If you want to re-run a bunch of code after every edit to a library, just put that code in a file and run it.

Eevee
  • 47,412
  • 11
  • 95
  • 127
1

Let's say there's a module named data.py that contains:

def foo():pass
In [13]: import data

In [14]: dir(data)
Out[14]: ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'foo']

Now add another function to data.py, while the shell is still running:

def foo():pass
def bar():pass
In [15]: import data

In [16]: dir(data)     #new function bar is not fetched by the import
                       #coz it simply loads the data object already present in memory

Out[16]: ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'foo']

Use imp.reload() to reload the module object.

In [17]: import imp

In [18]: imp.reload(data)
Out[18]: <module 'data' from 'data.py'>

In [19]: dir(data)
Out[19]: 
['__builtins__',
 '__doc__',
 '__file__',
 '__name__',
 '__package__',
 'bar',
 'foo']          # bar fetched

help() on reload():

reload(module) -> module

Reload the module. The module must have been successfully imported before.

phant0m
  • 16,595
  • 5
  • 50
  • 82
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • 1
    @CodeKingPlusPlus `from test import Test` is equivalent to `import test;Test=test.Test;del test`, so as there's no pre-loaded module in memory for `reload()` your code fails. – Ashwini Chaudhary Jan 14 '13 at 23:24