1

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

$python3 myscript.py arg1 &

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

$jupyter qtconsole &

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

$jupyter qtconsole myscript.py arg1 &

但它没有奏效。是否有可能做到这一点?如何?

4

1 回答 1

0

从一个新的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 不会将任何参数传递给内核,因为它无法对内核理解的参数做出任何假设。特别是,这意味着如果在 Jupyter 命令行上给出了选项 –debug,内核将不会收到该选项。

于 2017-02-08T22:37:55.167 回答