views:

479

answers:

2

I have been in love with zsh for a long time, and more recently I have been discovering the advantages of the ipython interactive interpreter over python itself. Being able to cd, to ls, to run or to ! is indeed very handy. But now it feels weird to have such a clumsy shell when in ipython, and I wonder how I could integrate my zsh and my ipython better.

Of course, I could rewrite my .zshrc and all my scripts in python, and emulate most of my shell world from ipython, but it doesn't feel right. And I am obviously not ready to use ipython as a main shell anyway.

So, here comes my question: how do you work efficiently between your shell and your python command-loop ? Am I missing some obvious integration strategy ? Should I do all that in emacs ?

+3  A: 

You can run shell commands by starting them with an exclamation mark and capture the output in a python variable. Example: listing directories in your /tmp directory:

ipy> import os
ipy> tmplist = !find /tmp
ipy> [dir for dir in tmplist if os.path.isdir(dir)]

The list object is a special ipython object with several useful methods. Example: listing files ending with .pdf

ipy> tmplist.grep(lambda a: a.endswith('.pdf')) # using a lambda
ipy> tmplist.grep('\.pdf$') # using a regexp

There is a lot of things you can do by reading the list of magic commands:

ipy> %magic

See the shell section of the Ipython documentation.

See also the IpythonShell section.

Toni
yeah, thanks, I had figured that out, my question was more of a "power user" one (even if I don't consider myself an ipython power user yet).For example, I'm quite annoyed that this exclamation mark syntax uses /bin/sh to execute commands, and not my usual shell. All my (shell) aliases, functions and customozations are thus lost. Any idea about how to make ipython use a different shell ?Another annoying feature is the tab-completion: for example, "run myscr<TAB>" will find "myscript.py" but not a file called "myscript". Long story short, I'm still not convinced by ipython...
Gyom
Did you try: ipython -p sh
Toni
Yes, but with no luck: obviously my shell aliases are still not there, and "run myscri<TAB>" still doesn't find "myscript" without .py suffix.I think that for the moment, I will stick to zsh in the terminal, and to the regular python interpreter in an emacs buffer. This way, I get proper completion everywhere, and I don't have to mess with ipython. maybe one day...
Gyom
+5  A: 

I asked this question on the zsh list and this answer worked for me. YMMV.

In genutils.py after the line

if not debug:

Remove the line:

stat = os.system(cmd)

Replace it with:

stat = subprocess.call(cmd,shell=True,executable='/bin/zsh')

you see, the problem is that that "!" call uses os.system to run it, which defaults to manky old /bin/sh .

Like I said, it worked for me, although I'm not sure what got borked behind the scenes.

chiggsy
yes, this indeed changes the shell for zsh ; but my .zshrc is still not read though, and I do not get my aliases/functions. do you ?
Gyom
.zshrc is only read in an interactive shell. /bin/zsh -i is how you force zsh to be interactive thus reading the .zshrc I wish I'd known this last year.
chiggsy