Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPython workflow (edit, run)

Is there a GUI for IPython that allows me to open/run/edit Python files? My way of working in IDLE is to have two windows open: the shell and a .py file. I edit the .py file, run it, and interact with the results in the shell.

Is it possible to use IPython like this? Or is there an alternative way of working?

like image 599
compie Avatar asked Aug 09 '10 08:08

compie


People also ask

How to edit a file in IPython?

IPython also provides edit magic command. It invokes default editor of the operating system. You can open it through Windows Notepad editor and the script can be edited. Once you close it after saving its input, the output of modified script will be displayed.

How do I load files into IPython?

A text file can be loaded in a notebook cell with the magic command %load . the content of filename.py will be loaded in the next cell. You can edit and execute it as usual. To save the cell content back into a file add the cell-magic %%writefile filename.py at the beginning of the cell and run it.


1 Answers

When I'm working with python, I usually have two terminal windows open - one with IPython, and the other with a fairly customized Vim.

Two good resources:

  • http://blog.dispatched.ch/2009/05/24/vim-as-python-ide/
  • http://dancingpenguinsoflight.com/2009/02/python-and-vim-make-your-own-ide/

Though it sounds like what you want is IPython's magic function %ed/%edit:

An example of what you can do:

In [72]: %ed IPython will make a temporary file named: c:\docume~1\wjwe312\locals~1\temp\ipython_edit_ar8veu.py 

In the file I put:

x = "Hello World" print 3 

After saving and quitting the file:

Editing... done. Executing edited code... 3 Out[72]: "x = 'Hello world'\nprint 3\n"  In [73]: x Out[73]: 'Hello world' 

You can define functions or anything else - just remember that the contents of the file will be executed when you close it.

Another similar workflow is to cd to the directory containing your Python script that you're editing with your favorite editor. Then you can %run the script from within IPython and you'll have access to everything defined in the file. For instance, if you have the following in the file test.py in your /home/myself directory:

    class Tester(object):         def __init__(self):             print "hi"      def knightme(name):         print "Hello, Sir ", name 

Then you can do the following:

In [42]: cd /home/myself /home/myself  In [43]: %run test.py # <Tab> autocomplete also works  In [44]: knightme('John') Hello, Sir  John  In [45]: t = Tester() Hi 

Either a mix or one of those workflows should give you something very similar to the way you're used to working in IDLE.

like image 157
Wayne Werner Avatar answered Sep 19 '22 16:09

Wayne Werner