Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to integrate ZSH and (i)python?

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 ?

like image 808
Gyom Avatar asked Jun 10 '09 03:06

Gyom


2 Answers

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.

like image 185
chiggsy Avatar answered Oct 21 '22 06:10

chiggsy


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.

like image 7
Toni Avatar answered Oct 21 '22 06:10

Toni