I would like to run an IPython instance on one machine and connect to it (over LAN) from a different process (to run some python commands). I understand that it is possible with zmq : http://ipython.org/ipython-doc/dev/development/ipythonzmq.html .
However, I can not find documentation on how to do it and whether it is even possible yet.
Any help would be appreciated!
EDIT
I would like to be able to connect to IPython kernel instance and send it python commands. However, this should not be done via a graphic tool (qtconsole) , but I want to be able to connect to that kernel instance from within a different python script...
e.g.
external.py
somehow_connect_to_ipython_kernel_instance
instance.run_command("a=6")
Update to minrk's answer after the split to jupyter. With jupyter_client (4.1.1) the simplest code is rather something like:
Note that:
Currently it is quite difficult to find an updated documentation; nothing yet on http://jupyter-client.readthedocs.org/en/latest/ for BlockingKernelClient. Some code in https://github.com/jupyter/jupyter_kernel_test. Any link welcome.
If you're using Anaconda, in OS X the JSON file is stored at
In Windows:
If you just want to connect interactively, you can use SSH forwarding. I didn't find this documented anywhere on Stack Overflow yet, yet this question comes closest. This answer has been tested on Ipython 0.13. I got the information from this blog post.
Run
ipython kernel
on the remote machine:Look at the
kernel-25333.json
file:Set up port-forwarding on the local machine:
Copy the
kernel-25333.json
file to the local machine:Run ipython on the local machine using the new kernel:
If you want to run code in a kernel from another Python program, the easiest way is to connect a BlockingKernelManager. The best example of this right now is Paul Ivanov's vim-ipython client, or IPython's own terminal client.
The gist:
IPYTHONDIR/profile_<name>/security/kernel-<id>.json
, which contain information necessary for various clients to connect and execute code.A working example:
In a shell, do
ipython kernel
(oripython qtconsole
, if you want to share a kernel with an already running GUI):This wrote the 'kernel-6759.json' file
Then you can run this Python snippet to connect a KernelManager, and run some code:
The output of a run:
see the message spec for more information on how to interpret the reply. If relevant, stdout/err and display data will come over
km.iopub_channel
, and you can use the msg_id returned byshell.execute()
to associate output with a given execution.PS: I apologize for the quality of the documentation of these new features. We have a lot of writing to do.
The above answers are a bit old. The solution for the latest version of
ipython
is much simpler but is not documented well at one place. So I thought I would document it here.Solution to connect from any OS to a ipython kernel running on
Windows
If either the client or server is a
linux
or other operating system, just change the location ofkernel-1234.json
appropriately based on Where is kernel-1234.json located in Jupyter under Windows?ipykernel
is installed usingpip install ipykernel
ipykernel
usingipython kernel -f kernel-1234.json
kernel-1234.json
file on yourWindows
machine. The file will probably have a different number, not1234
and will most likely be located in 'C:\Users\me\AppData\Roaming\jupyter\runtime\kernel-1234.json': https://stackoverflow.com/a/48332006/4752883pip install jupyter-console
orpip install qtconsole
https://jupyter-console.readthedocs.io/en/latest/ipconfig
to find out the ip address of your Windows server. (On Linux do aifconfig
at the shell prompt). In thekernel-1234.json
file change the ip address from127.0.0.1
to the ip address of your server. If you are connecting from anotherWindows
server, then copy thekernel-1234.json
file to your local computer and note down the path.kernel-1234.json
and start Jupyter Console usingjupyter console --existing kernel-1234.json