Search code examples
pythonmodulereloadpython-idle

How to reload Python module in IDLE?


I'm trying to understand how my workflow can work with Python and IDLE.

Suppose I write a function:

def hello():
    print 'hello!'

I save the file as greetings.py. Then in IDLE, I test the function:

>>> from greetings import *
>>> hello()
hello!

Then I alter the program, and want to try hello() again. So I reload:

>>> reload(greetings)
<module 'greetings' from '/path/to/file/greetings.py'>

Yet the change is not picked up. What am I doing wrong? How do I reload an altered module?

I've been reading a number of related questions on SO, but none of the answers have helped me.


Solution

  • You need to redo this line:

    >>> from greetings import *

    after you do

    >>> reload(greetings)

    The reason just reloading the module doesn't work is because the * actually imported everything inside the module, so you have to reload those individually. If you did the following it would behave as you expect:

    >>> import greetings
    >>> greetings.hello()
    hello!
    

    Make change to file

    >>> reload(greetings)
    <module 'greetings' from 'greetings.py'>
    >>> greetings.hello()
    world!