[Python-ideas] a standard approach to module-replace-itself-in-sys.modules

Brett Cannon brett at python.org
Thu Mar 13 13:54:16 CET 2014


On Thu, Mar 13, 2014 at 4:01 AM, Eric Snow <ericsnowcurrently at gmail.com>wrote:

> Not that long ago we had a discussion about the esoteric, yet totally
> supported and not going anywhere, trick of having a module replace
> itself in sys.modules.  Presently there isn't any convention
> surrounding the technique.  It's simply a matter of sticking something
> else into sys.modules[__name__].
>
> The problem is that there are some subtle things that can go wrong in
> the import system if you aren't careful and people generally won't
> know that.  (Then again the whole technique isn't exactly in the
> mainstream!)  To help reduce the risk here, I propose that we provide
> a helper in importlib.util and an associated convention.
>

Do we actually want to promote this practice by codifying support for it in
the stdlib? If you're doing this to get decorators for module attributes
then you should say this is for that goal and the solution is getting the
module swapping hack to be easier to use (and might call for a better
solution). Otherwise this proposal feels like it's trying to codify
behaviour that is rarely necessary.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140313/18b8eb42/attachment.html>


More information about the Python-ideas mailing list