[IPython-dev] Where should __future__ statements affect the interactive environment.

Thomas Kluyver takowl at gmail.com
Wed Apr 11 10:48:41 EDT 2012


Pierre Haessig has spotted inconsistencies in the effect of a
statement like 'from __future__ import division'. If it's in a startup
file with a .py extension, it's local to that file; if the same file
has a .ipy extension, it affects the interactive environment. It
occurs to me that we haven't given this proper consideration. There
are a lot of places that we execute external code:

* At startup: `exec_lines`, `exec_files`, startup files. There are .py
and .ipy files, and they should definitely behave the same way with
future statements.
* `%run` (and `%run -i`), `%rerun`, `%edit`, macros, `%loadpy`
* Extensions
* Code loaded by the autoreload extension

My gut feeling is that of those, `exec_lines`, `%loadpy`, macros and
maybe `%run -i` should 'leak' future statements to the interactive
environment. Everything else should keep future statements to itself
(except by using `run_cell()` to run them through IPython). But I'm
asking here to get other opinions.

Technical details: future statements act via Python's bytecode
compiler, not the virtual machine. We use the rather niche 'codeop'
module from the standard library, which offers a compiler that
remembers future statements from one compilation to the next. So code
that is passed to ip.compile() leaks __future__ statements, while code
passed to the builtin compile() or exec() doesn't.

This is a continuation of my discussion with Pierre on PR #950:
https://github.com/ipython/ipython/pull/950

Thomas



More information about the IPython-dev mailing list