[IPython-dev] Sth. like "python -O" within IPython?

Fernando Perez fperez.net at gmail.com
Mon May 8 14:21:15 EDT 2006


On 5/8/06, Hans Meine <hans_meine at gmx.net> wrote:
> Hi!
>
> When I am using assert's a lot, it certainly makes a difference when I run my
> program with "python -O".  Is it possible to run IPython with optimization?
>
> I would imagine either
>
> * a "global" commandline parameter -O or
>
> * an interactive means to switch on optimization (e.g. -O for %run or
> similar), if that is possible.

A bit of googling so far doesn't really reveal anything useful.  I've
been trying to find out if it's possible in python to compile source
strings (via compile() ) into optimized bytecode, and if it is
possible for execfile() to run the given filename with optimizations
active.

So far, all documentation seems to (mostly by omission) indicate that
such local control of optimization is not possible in python, and that
the -O state is a global of the interpreter itself.  If this
interpretation is correct, it means that you would need to run /all/
of ipython with optimization active for this to work.  This part is
easy:

planck[~]> ls ~/ipython/ipython/IPython/*.pyo | wc -l
/bin/ls: No match.
0
planck[~]> python -O `which ipython`
[... start ipython session]

planck[~]> ls ~/ipython/ipython/IPython/*.pyo | wc -l
36

This shows that indeed, the .pyo files were created and ipython itself
is running under -O.   Furthermore, this activates -O for all files
executed with %run:

planck[~/test]> cat optim.py
def f():
    assert 0,"This should always fail if asserts are on"
    print "If this prints, asserts are being ignored"

f()

And then:

planck[~/test]> ip
Python 2.3.4 (#1, Feb  2 2005, 12:11:53)
Type "copyright", "credits" or "license" for more information.

IPython 0.7.2.rc1 -- An enhanced Interactive Python.
?       -> Introduction to IPython's features.
%magic  -> Information about IPython's 'magic' % functions.
help    -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: run optim.py
---------------------------------------------------------------------------
exceptions.AssertionError                            Traceback (most
recent call last)

/home/fperez/test/optim.py
      3     print "If this prints, asserts are being ignored"
      4
----> 5 f()
      6
      7

/home/fperez/test/optim.py in f()
      1
----> 2 def f():
      3     assert 0,"This should always fail if asserts are on"
      4     print "If this prints, asserts are being ignored"
      5
      6 f()

AssertionError: This should always fail if asserts are on
WARNING: Failure executing file: <optim.py>

#########

Now, let's run with -O:


planck[~/test]> python -O `which ipython`
Python 2.3.4 (#1, Feb  2 2005, 12:11:53)
Type "copyright", "credits" or "license" for more information.

IPython 0.7.2.rc1 -- An enhanced Interactive Python.
?       -> Introduction to IPython's features.
%magic  -> Information about IPython's 'magic' % functions.
help    -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: run optim.py
If this prints, asserts are being ignored

###########

So you could alias ipythono to 'python -O path/to/your/ipython/' and
use it this way.  I hope this is useful, but if anyone knows how to
(or if it's even possible to) enable optimizations locally, I'd be
interested in hearing about it.

Cheers,

f




More information about the IPython-dev mailing list