启动外部jupyter qtconsole并运行带有args的脚本



我有一个脚本,它需要一个参数来运行。通常,我可以使用以下命令使用标准python控制台从终端运行它:

$python3 myscript.py arg1 &

但我想在jupyter qtconsole中执行该脚本。可以通过以下命令从终端启动外部控制台:

$jupyter qtconsole &

我尝试启动jupyter qtconsole并使用参数arg1运行myscript.py,使用类似的方法:

$jupyter qtconsole myscript.py arg1 &

但这并没有奏效。有可能做到这一点吗?怎样

从新的qtconsole:

In [14]: sys.argv
Out[14]: 
['/usr/local/lib/python3.5/dist-packages/ipykernel/__main__.py',
'-f',
'/run/user/1000/jupyter/kernel-25521.json']
In [16]: %connect_info
{
"stdin_port": 57643,
"key": "bcc03e84-7c43-4fbe-84d7-6c005aa37930",
"ip": "127.0.0.1",
"transport": "tcp",
"iopub_port": 45064,
"hb_port": 46748,
"control_port": 39960,
"kernel_name": "",
"shell_port": 57532,
"signature_scheme": "hmac-sha256"
}
Paste the above JSON into a file, and connect with:
$> jupyter <app> --existing <file>
or, if you are local, you can connect with just:
$> jupyter <app> --existing kernel-25521.json
or even just:
$> jupyter <app> --existing
if this is the most recent Jupyter kernel you have started.

因此,如果我不

$ jupyter console --existing kernel-25521.json

我得到了一个控制台,它与qtconsole共享内核。如果我在一个模块中导入一个模块或创建一个变量,那么它在另一个中是可用的。

通常,当我想在ipython中运行脚本时,我会使用%run魔术,而不是试图将其包含在命令行中。

In [32]: %run echo_argv.py testing 1 2 3
['echo_argv.py', 'testing', '1', '2', '3']

代替外壳

$ipython3 -i echo_argv.py -- testing 1 2 3

为了在没有任何进一步交互的情况下运行脚本,我使用常规的$python调用。不需要涉及jupyteripython

$ python3 echo_argv.py -- testing 1 2 3

qtconsole配置选项:

KernelManager.kernel_cmd:列表默认值:[]

已弃用:请改用kernel_name。

启动内核的Popen命令。如果您有一个自定义内核,请覆盖它。如果在配置文件中指定了kernel_cmd,Jupyter不会向内核传递任何参数,因为它无法对内核理解的参数做出任何假设。特别是,这意味着内核不会收到选项-debug,如果它是在Jupyter命令行上给出的。

最新更新