Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run python script using existing ipython kernel without console

I can run a python script from a bash shell like so:

>> python script.py

I can also start an iPython kernel and connect multiple iPython consoles to the same kernel like so:

>> ipython kernel
...
To connect another client to this kernel, use:
--existing kernel-8987.json

then, for as many consoles as I would like, I execute

>> jupyter console --existing kernel-8987.json

However, what I would like to do is start a kernel, but then run scripts without opening a console. I'd like to do something like this:

>> ipython --existing kernel-8987.json script.py

Is this possible to do this somehow?

like image 568
mrclary Avatar asked Dec 24 '16 21:12

mrclary


People also ask

How do I run a Python file in IPython terminal?

You can use run command in the input prompt to run a Python script. The run command is actually line magic command and should actually be written as %run. However, the %automagic mode is always on by default, so you can omit this.


1 Answers

Extending on the other answer and use the %run magic command1, it's possible to do this (which works for multiple-line scripts):

$ jupyter console --simple-prompt --existing kernel-8987.json <<< '%run script.py'

or (on Windows where <<< doesn't work)

> echo %run script.py | jupyter console --simple-prompt --existing kernel-8987.json

Unfortunately, this method still prints some redundant prompt (In[1]:) on the console.


Alternatively, using Python API: (create a script to execute this Python code) (on my machine, this method is much faster)

from jupyter_client.manager import KernelManager
manager=KernelManager(connection_file="full path to connection file.json")
manager.load_connection_file()
manager.client().execute("commands to execute")

The commands to execute might span multiple lines, or have the form %run file.py.

There's also silent=True, store_history=False as parameters to execute().

The full path to the connection file can be found as

  • Path(jupyter_core.paths.jupyter_runtime_dir()) / "kernel-8987.json", or
  • jupyter_client.connect.find_connection_file("kernel-8987.json") (find file with specific name), or
  • jupyter_client.connect.find_connection_file() (find the latest file)

See also jupyter-client documentation.

Disclaimer: Some of the code are taken from jupyter-vim plugin, and manager.load_connection_file and connect.find_connection_file appears to be undocumented.


1: As mentioned in the documentation, the file is executed in a new namespace unless you use -i.

like image 92
user202729 Avatar answered Nov 15 '22 08:11

user202729