【问题标题】:Launch external jupyter qtconsole and run a script with args启动外部 jupyter qtconsole 并使用 args 运行脚本
【发布时间】:2017-02-08 16:24:49
【问题描述】:

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

$python3 myscript.py arg1 &

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

$jupyter qtconsole &

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

$jupyter qtconsole myscript.py arg1 &

但它没有奏效。是否有可能做到这一点?怎么样?

【问题讨论】:

  • 不完全是jupyter,但这有效:ipython3 -i echo_argv.py -- testing 1 2 3,启动一个控制台,运行each_argv.py 并将-- 之后的参数传递给它。但是jupyter qtconsole 不接受那些添加的字符串。
  • 感谢您的评论!我尝试了类似的解决方案,但我确实需要使用jupyter qtconsole
  • 在控制台/内核jupyter 两部分配置中,添加脚本和参数信息可能没有意义。谁使用它,控制台还是内核(运行 Python)?我想知道config文件中是否有任何有用的信息。
  • 命令$jupyter qtconsole & 在新窗口中打开一个新控制台。事实上,我正在尝试编写一个 bash 脚本,该脚本启动具有不同参数的相同 python 脚本(args 用于 python 脚本),然后观察在多个控制台窗口中运行的执行。

标签: python bash jupyter qtconsole


【解决方案1】:

来自新的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 配置选项:

内核管理器.kernel_cmd : 列表 默认值:[]

已弃用:改用 kernel_name。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    • 2018-07-12
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多