Using IPython as your Python REPL
I had heard about IPython on my Twitter feed from people like Titus Brown but only in the context of IPython Notebooks for sharing data and calculations. I had been meaning to look into it because I figured at some point someone would want our group at work to do something about sharing how to do scientific analyses, etc. and that knowing IPython might come in handy. What I didn’t know is that you can use IPython - without the notebook component - as a replacement for the standard python REPL, kind of like how lots of people use pry instead of Ruby’s built in irb.
I wish I had looked into this sooner. IPython is more like the python REPL I needed when I was starting out in Python. I haven’t had a chance to use it a lot - for one thing I installed IPython in my virtual environment and my current environment doesn’t really have anything in it to play with. But already I am hopeful.
The main thing that makes running Python in a shell inside emacs so
painful is that copying code and pasting into the emacs window makes
the indenting go all weird. Can ipython fix that? I am not sure. But
it does have a
%paste macro. Does that help? Perhaps it helps for
pasting with the paste buffer. But my usual emacs workflow of copying
a chunk of code into my kill ring and then yanking it into the shell
is still broken due to whitespace issues (unless of course the code is
actually not indented, which rarely happens). Perhaps
will help - at least to get some code into my REPL so I can play with it.
ipython has a nice readline history feature. One nice option is being
able to get the output from the last 3 commands you ran with:
I am also very impressed with being able to run system commands and
assign the output to python variables, e.g. %cd /Users/me myfiles = %ls .bash*
Or something like:
To get specific information on an object, you can use the magic
%psource and ```%pfile`` OMG!
This is like pry’s cd into an object and look around. Instructions for
using these magic commangs as part of some other python program
are in the docs. This seems to me to be a lot like binding.pry to
start a REPL somehwhere.
%automagic is turned ON by default, you can often just do
cd without the leading
%. You can toggle the
automagic by typing
%automagic until you get what you want.
To see what things are included in automagic: %magic -brief
There is also a set of docs on how to use ipython as a systems shell: https://ipython.org/ipython-doc/dev/interactive/shell.html
Reloading files inside the REPL
IPython.lib.deepreload module allows you to recursively
reload a module: changes made to any of its dependencies will be
reloaded without having to exit. To start using it, do:
The automatic quoting
sounded useful but when I played around with it, it was odd. All I
really want is an equivalent of Ruby’s
%w(foo bar baz) so I don’t have
to type a boatload of “ and , to get
["foo", "bar", "baz"].
Unfortunately this is not that feature.
Spying on IPython
Or “How IPython keeps track of what you have running where”. When the ipython notebook started up, it created the nbserver-###.json file. When I started actually running code in an .ipynb file, then it needed a running kernel - so it spun one up and created the kernel=###.json file
This part of the docs has lots of useful information about how ipython processes communicate with the kernel - message passing, security (only bind to localhost OR use ssh tunnels to encrypt trafic and keep it private).