how to re-import changed code?

John Roth newsgroups at jhrothjr.com
Fri Oct 15 21:06:15 EDT 2004


"Jeff Shannon" <jeff at ccvcorp.com> wrote in message 
news:10n0banri5f2bda at corp.supernews.com...
> Wolfgang.Stoecher at profactor.at wrote:
>
>>Hi,
>>
>>another beginner's question:
>>when I import a module, change the corresponding textfile and try to 
>>import the same module again, it looks like the internal code does not 
>>change (the source-line in an error-message is up to date, however!). Also 
>>del module before import and deleting the .pyc file do not help! How to 
>>realize a fast edit-try-cycle? (btw: I am using python 2.2 under windows)
>>
>
> What you're looking for is the reload() function.
>
> Normally, an imported module is cached by the interpreter, and subsequent 
> imports go straight to the cache without checking whether the file has 
> changed or not.  If you reload(), that specifically clears the cache and 
> imports from the (current) disk file again.

That works, but with a very important caveat: reload() does not
change anything in the module that's currently being referred to.
This can lead to very subtle bugs, and all IDE/debugger combinations
don't handle it properly.

What I do is keep a command line open under Windows, and
I create a .cmd file with a very short name. It takes about three
keystrokes to run the test (alt-tab, t, return), and one keystroke
to get back to the IDE (alt-tab). This reloads my unit test suite
and runs it.

John Roth


>
> Jeff Shannon
> Technician/Programmer
> Credit International
> 




More information about the Python-list mailing list