使用KernelClient API在ipython内核中执行代码 [英] Executing code in ipython kernel with the KernelClient API

查看:120
本文介绍了使用KernelClient API在ipython内核中执行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的ipython内核,带有通信文件'path/comm_file.json',并且我想使用Kernel Client API在此内核中执行代码(实际上我并不挑剔,任何方法都可以.) .我了解这是从jupyter 文档.因此,我编写了以下代码:

I have an existing ipython kernel, with a communication file 'path/comm_file.json' and I want to execute code in this kernel using the Kernel Client API (actually I'm not picky, any method will do..). I understood that this is the best way to do things from the jupyter documentation. So I write the following code:

from jupyter_client import KernelClient
client = KernelClient(connection_file='path/comm_file.json')
client.execute('a = 10')

但是execute方法导致以下错误:

But the execute method leads to the following error:

  File "C:\Python27\lib\site-packages\jupyter_client\client.py", line 249, in execute
    self.shell_channel.send(msg)
  File "C:\Python27\lib\site-packages\jupyter_client\client.py", line 143, in shell_channel
    socket, self.session, self.ioloop
TypeError: object.__new__() takes no parameters

我在这里做错什么了吗?

What am I doing wrong here??

推荐答案

我能够使用以下方法为我完成一个简单的KernelClient工作:

I was able to make a simple and bare KernelClient work for me with this:

from jupyter_client.blocking import BlockingKernelClient

kc = BlockingKernelClient(connection_file='path/comm_file.json')

kc.load_connection_file()
kc.start_channels()

msgid = kc.execute('a = 10')
reply = kc.get_shell_msg(timeout=5)

JupyterConsoleApp (由<给出现有内核文件后,a href ="https://github.com/jupyter/jupyter_console/tree/master/jupyter_console" rel ="nofollow"> jupyter_console 初始化其客户端.

这篇关于使用KernelClient API在ipython内核中执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆