20

When writing or debugging a Python program, I really like using the -i command line switch to be able to directly inspect my functions without having to run everything from start to finish.

However, whenever I make a change to the code I have to close and restart my interactive session, losing all temporary variables that I might have defined. How do I reload my source file from within the python interpreter?


The builtin function reload looks like it was made for this, but I can only use it with named modules:

>> import my_prog >> print my_prog.x -- prints an error, because x is not defined -- -- edited my_prog.py to add the x global now... >> reload(my_prog) >> print my_prog.x -- prints x 

However, if I instead to do a from my_prog import * in the beginning reload doesn't work, and doing the import again also has no effect.

3 Answers 3

25

This has to do with the way Python caches modules. You need a module object to pass to reload and you need to repeat the import command. Maybe there's a better way, but here's what I generally use: In Python 3:

>> from importlib import reload >> import my_prog >> from my_prog import * *** Run some code and debug *** >> reload(my_prog); from my_prog import * *** Run some code and debug *** >> reload(my_prog); from my_prog import * 

In Python 2, reload is builtin, so you can just remove the first line.

Sign up to request clarification or add additional context in comments.

2 Comments

Well thanks! @smaplebias has a more thorough description for those who are interested in the finer details. +1 from me.
This is a useful trick, thanks. Beware of stale/leftover symbols though: suppose you have a function foo() that calls bar() and you decide to rename bar to baz. Only you forgot to edit foo's body, so it still calls bar() (which is still valid after a reload). Before you know it, you've spent half a day debugging this.
12

When you use from my_prog import * you're pulling symbols into the interpreter's global scope, so reload() can't change those global symbols, only module-level attributes will be changed when the module is recompiled and reloaded.

For example: myprog.py:

x = 1 

In interepreter:

>>> import myprog >>> myprog.x 1 >>> from myprog import x >>> x 1 

Now edit myprog.py setting x = 2:

>>> reload(myprog) >>> myprog.x 2 >>> x 1 

Repeat the from myprog import * to pull the symbols to global scope again:

>>> reload(myprog) >>> from myprog import * 

1 Comment

This is all kind of obsolete without mentioning python 3's breaking change: stackoverflow.com/a/10142772/1509695
1

I got it. For example we enter interative mode then reload the source file using importlib package like this:

from importlib import reload reload(filename) # Need no quotatiom mark 

1 Comment

This doesn't seem to work anymore. It works with import filename but not with python3 -i filename

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.