IPython workflow (edit, run)
Asked Answered
N

14

51

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?

Nguyetni answered 9/8, 2010 at 8:50 Comment(2)
See https://mcmap.net/q/53172/-what-ide-to-use-for-python-closed and similar SO discussions.Tarrah
None of the acronyms used on that page matches what I'm looking for...Nguyetni
Y
55

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:


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.

Yean answered 20/8, 2010 at 13:59 Comment(15)
If you just want to edit a file from ipython without executing it, do %edit -x <file>.Bulbul
ma3204, that is incredibly useful! I'm probably barely competent with IPython - I just use it like an advanced "regular" shell. It's got a lot of features I just don't know about.Yean
But is there a way to use IPython within IDLE? Because the standard interpreter sucks, and the Windows Command Prompt sucks. I want the IPython interpreter in a console that actually works well with it.Protraction
@endolith, You can always install cygwin ;) - Actually I used Console2 for a while and it worked pretty well.Yean
@Wayne What would installing Cygwin accomplish?Protraction
@endolith, not having to use windows command prompt, of course.Yean
Oh. That's what I want, but why does that require installing an entire operating system? Can't alternate terminals be written by themselves?Protraction
I guess technically speaking it would be possible - but I'm not aware of any real efforts to do such things under Windows. This is probably the only thing that I've seen - and you can actually set it up to use IPython as the shell, and you can do most things in IPYthon that you can do inside the cmd.exe, so that might be useful for you.Yean
@endolith, IdleX supports running IPython within IDLE.Smell
The problem is, how can you RE-EDIT the result you had in 72? %edit 72, or %recall 72 just gives the %edit-line. It's so irritating to always loose the work, not being able to re-run.Backwash
To answer myself: you do %edit _72 to get the output! :D Awesome. Finally.Backwash
But can you send selected code to IPython from vim?Despairing
@Despairing the quickest answer i have is to use tmux send-keys, but i bet ipython provides another wayYean
@odinho-Velmont, thanks for the tip about the %edit _72.. %edit is pretty good, but without this tip, its pretty much useless.. :)Boong
@Alcott, you can use vim-ipython. It uses tmux to send code blocks to ipython. It works pretty well on linux.. but probably can be made to work on Windows with some work.Boong
S
17

Spyder, previously known as SPyderlib / Spyder2

Pretty lightweight, fast and support almost all features you will ever need to work with a python project. It can edit and run .py files in an embedded IPython instance and then interact with them, set breakpoints, etc.

enter image description here full-size

Scattering answered 15/10, 2010 at 2:29 Comment(0)
M
5

Try Spyder, I have spent all day trying to find an IDE which has the functionality of ipython and Spyder just kicks it out of the park..

Autocomplete is top notch right from install, no config files and all that crap, and it has an Ipython terminal in the corner for you to instantly run your code.

big thumbs up

Meissen answered 12/7, 2012 at 16:24 Comment(0)
S
4

Take a look at DreamPie. Might be what you are looking for.

Scurf answered 17/10, 2010 at 7:1 Comment(0)
B
3

Personally, I like PyScripter. Unfortunately, it only works on Windows, but also runs perfectly in Wine.

Bloke answered 18/2, 2011 at 1:10 Comment(0)
I
2

If you like the work-flow under Matlab, then you probably should try the following two:

1, Try the combination of Spyder and Vim.

  • Edit python files in Vim (Spyder can reload the file automatically)

  • Run the code in Spyder (in the same interpreter, which is important for me):

  • Use F9 to run the current file

  • Ctrl+F9 to run the selected block

2, Use Vim + conque-shell. (on google code)

  • Open your preferred Python interpreter in Vim,

    e.g., just :ConqueTermSplit python.

  • then visual select some Python code

  • press F9 to paste and run it in the Python interpreter buffer.

Note: a few more:

  • :ConqueTermVSplit python,

  • :ConqueTerm python

  • :ConqueTermVSplit rlwrap python

    If your interpretor misses readline, you can use rlwrap.

Intendance answered 8/1, 2011 at 10:37 Comment(0)
A
2
sudo apt-get install ipython

Once you are done with installing ipython.

Start ipython from terminal (just hit ipython in the ternminal)

To run ravi.py file all you need to do is

%run ravi.py
Actinomycete answered 10/3, 2012 at 1:49 Comment(0)
S
2

The latest version of IdleX supports IPython within IDLE, as well as the %edit magic. You can run your files from the IDLE editor within the IPython shell many ways, either by F5 (run everything), F9 (run a selection), or Ctrl+Enter (run a subcode).

Smell answered 26/3, 2012 at 7:18 Comment(0)
D
1

You might like PySlices...

It's kind of a shell/editor hybrid that lets you save your session as special (barely) modified python files called .pyslice files.

It's now part of wxPython, so just install that (v2.8.11 or later) and run "python -m wx.py.PySlices" on the command line to launch it.

That said, I still end up using an external editor for scripts (geany).

Durant answered 1/6, 2011 at 16:32 Comment(0)
R
1

I want to suggest excellent plugin for vim that makes two-way integration between Vim and IPython: vim-ipython.

From project page on http://github.com/ivanov/vim-ipython:

Using this plugin, you can send lines or whole files for IPython to execute, and also get back object introspection and word completions in Vim, like what you get with: object? and object. in IPython.

This plugin has one big limitation: it doesn't support python 3 (it's planned).

Rant answered 15/11, 2013 at 20:19 Comment(0)
P
0

Personally, I use what @Wayne suggested, a combination of vim and ipython...

However, if you'd prefer a different approach, take a look at spyder.

As of the latest version (1.1) ipython should be fully integrated. If you download an earlier version, things will work fine with ipython as an external shell, but you won't get a few of spyder's nifty features (like viewing all of the currently defined variables in the workspace window).

Spyder is definitely a bit heavyweight, but it's an interesting project.

Another (very, very, new) similar project to take a look at is iep. It will (sort-of) work with ipython as shell, and I'd be willing to be bet that nicer ipython integration will be along before too long. At any rate, iep is essentially a more lightweight alternative to spyder.

Both of these are oriented towards scientific computing, and so have nice integration with things like matplotlib (and thus can automatically run gui main loops in a seperate thread). They're not quite like "normal" IDE's but they may fill the niche you're looking for quite nicely.

Pescara answered 24/8, 2010 at 16:50 Comment(0)
L
0

You can use the autoreload module in IPython to automatically reload code.

Open jupyter qtconsole or jupyter console and type:

%load_ext autoreload
%autoreload 2
from your_work_file import *

Now every time you save your_work_file.py, it will be automatically reloaded.

Hint: if you want this to happen automatically, put the followinglines in your ipython_config.py file:

c.InteractiveShellApp.extensions = ['autoreload']         
c.InteractiveShellApp.exec_lines = ['%autoreload 2']
Lionfish answered 20/1, 2016 at 17:2 Comment(0)
B
0

Try Ptpython. It has much better integration with VIM. You can directly edit in VIM by just pressing V. It also allows browsing your history.. so you can pretty much code in the shell, and incrementally build up your code.

If you are already familiar with ipython, you can check the advantages of ptpython here:

https://www.youtube.com/watch?v=XDgIDslyAFM

Boong answered 10/1, 2018 at 18:37 Comment(0)
F
0

I just use the exclamation mark (!) to run vi as a shell command

In [1]: !vi myScript.py

and when done with editing I just quit vi to get back to the Ipython shell.

To run the script one can then use

In [2]: %run myScript.py

as suggested in another answer and not !python ... because the Python version in ipython might be different from the one in the underlying shell.

If you want to dump some code in a file use the magic %%writefile

 In [3]:%%writefile myScript.py
    ...: print("hello")
    ...: 
    ...: 

Be careful because this will overwrite myScript.py. To append use %%writefile -a.

Frieda answered 11/3, 2022 at 14:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.