[IPython-user] %run foo.ipy crashes on blank lines, but %edit foo.ipy works OK

Matthew Wilson matt@tplus1....
Tue Jun 3 10:46:58 CDT 2008

I find myself writing scripts that depend on a highly-customized ipython
session to run.

For example, my ipython session creates database connections and loads
lots of modules.

Then my script just does the interesting stuff.

First I start up ipython with my specialized profile:

    ipython -p with-database-connection

then in ipython, I run %edit blah.py

And then immediately close the editor.

Then all the objects in blah.py are now in the top-level namespace, and
I can run everything.

It's a minor detail, but using %edit to open and then immediately close
the editor seems silly.

I tried %run, but that runs my script in an empty namespace, and then
populates the top level.  No good.

I read the help for %run and I see how I can name my file blah.ipy.

That is OK.  However, it seems like functions with blank lines cause a
syntax error.  I put this function in blah.ipy:

    def f(x):
        "Return x squared."

        print "x is %s." % x
        return x * x

Then I tried to run the file, and I got this error?

    In [77]: %run /home/matt/blah.ipy
       File "<ipython console>", line 3
    SyntaxError: 'return' outside function (<ipython console>, line 3)

Meanwhile, %edit blah.ipy loads the file just fine.

Any ideas?


Programming, gardening, life in Cleveland, OH 

More information about the IPython-user mailing list