[IPython-dev] Extension loading

Jason Grout jason-sage at creativetrax.com
Sat Sep 29 13:47:58 EDT 2012


On 9/29/12 10:42 AM, Thomas Kluyver wrote:
> This grows out of a discussion on PR #2430
> https://github.com/ipython/ipython/pull/2430
>
> Some of our default extensions contain a global flag to prevent the
> extension being loaded more than once. This is rather ugly, and fixes
> us to the singleton model of one IPython shell per process. I'd like
> to clear that up before more third parties copy the pattern.
>
> One obvious way is for the extension manager to track what extensions
> are loaded, and refuse to load an extension more than once. That has a
> downside, though: you might want calling %load_ext more than once to
> work. For instance, if the extension adds something to the namespace,
> the user overwrites it, and then realises they didn't want to, calling
> %load_ext again is an obvious course of action. We have a %reload_ext
> magic function, but the user may not know about it.
>
> So, I see three options:
> 1. The extension manager doesn't track what's loaded, and it remains
> up to the extension to avoid loading twice if it wants to.*
> 2. The extension manager tracks what's loaded, and refuses to load
> anything more than once. It's up to the user to call %reload_ext if
> they need to reload something.
> 3. The extension manager tracks what's loaded, and calling %load_ext a
> second time works like %reload_ext (optional: get rid of %reload_ext,
> which has become redundant)
>
> * Are there any cases where running the extension-loading code more
> than once without reloading the module causes something to go wrong?

In the preliminary Sage extension, we do things like add a pre_run_code 
hook and run a startup file.  If we register a hook twice, I suppose it 
runs twice?

By the way, we use a decorator to implement the run-once functionality: 
https://gist.github.com/3804691

This decorator might be a good thing to add to the utility file if you 
still support the singleton model.

Thanks,

Jason




More information about the IPython-dev mailing list