远程jupyter内核-不同的虚拟环境?

时间:2019-04-17 14:57:11

标签: python python-3.x jupyter-notebook

我使用软件包remote_ikernel从azure VM自动连接到azure VM。这两个系统是相同的,也就是说,它们具有相同的python环境。

我使用

启动远程内核
ssh -oStrictHostKeyChecking=no {username}@{ip} sudo /anaconda/envs/py36/bin/ipython kernel

然后通过在下面的脚本中填充参数来更新本地kernel.json

        {{
    "argv": [
        "/anaconda/envs/py35/bin/python",
        "-m",
        "remote_ikernel",
        "--interface",
        "ssh",
        "--host",
        "{username}@{ip}",
        "--kernel_cmd",
        "/anaconda/envs/{remote_python}/bin/ipython kernel -f {{host_connection_file}}",
        "{{connection_file}}"
    ],
    "display_name": "SSH {username}@{kernel_name}",
    "remote_ikernel_argv": [
        "/anaconda/envs/py35/bin/remote_ikernel",
        "manage",
        "--add",
        "--kernel_cmd=/anaconda/envs/{remote_python}/bin/ipython kernel -f {{connection_file}}",
        "--name=Remote VM",
        "--interface=ssh",
        "--host={username}@{ip}"
    ]
}}

首次尝试 但是,我们不想使用远程/anaconda/envs/py35/bin/python,而要使用其py36兄弟。我希望通过在第一个命令中对其进行调整来解决该问题,但这并不能解决。它也无助于更改kernel.json中的任何py35。我们如何实现这一目标?

第二次尝试(编辑) 我修改了kernel.json,使其包含指向py36环境的指针。请参阅上面的更新版本。下面的日志看起来不错,但是import sys; sys.executable仍然显示py35版本。

[I 11:54:57.549 remote_ikernel] Launching kernel over SSH.
[I 11:54:57.549 remote_ikernel] Login command: 'ssh -o StrictHostKeyChecking=no  username@ip'.
[I 11:54:57.889 remote_ikernel] Established connection; starting kernel.
[I 11:54:57.889 remote_ikernel] Current working directory /data/projects/...
[I 11:54:58.040 remote_ikernel] Running kernel command: '/anaconda/envs/py36/bin/ipython kernel -f ./rik_kernel-40d44e0d-4dac-4939-b018-74f4c82b6855.json'.
[I 11:54:59.357 NotebookApp] Adapting to protocol v5.1 for kernel 40d44e0d-4dac-4939-b018-74f4c82b6855
[I 11:54:59.358 NotebookApp] Restoring connection for 40d44e0d-4dac-4939-b018-74f4c82b6855:d877b8cff59245bf9ca5811d9310ee7f
[I 11:54:59.358 NotebookApp] Replaying 7 buffered messages
[I 11:55:28.382 remote_ikernel] Setting up tunnels on ports: 57033, 45674, 57305, 38105, 56085.

显示的远程内核文件

 {
  "shell_port": 45674,
  "iopub_port": 57305,
  "stdin_port": 38105,
  "control_port": 56085,
  "hb_port": 57033,
  "ip": "127.0.0.1",
  "key": "9e336436-...",
  "transport": "tcp",
  "signature_scheme": "hmac-sha256",
  "kernel_name": ""
}

2 个答案:

答案 0 :(得分:0)

kernel_cmd是建立远程连接后运行的,因此要在远程计算机上获得py36版本的ipython,您的kernel.json应该看起来像:


{
    "argv": [
        "/anaconda/envs/py35/bin/python",
        "-m",
        "remote_ikernel",
        "--interface",
        "ssh",
        "--host",
        "{username}@{ip}",
        "--kernel_cmd",
        "/anaconda/envs/py36/bin/ipython kernel -f {host_connection_file}",
        "{connection_file}"
    ],
    "display_name": "SSH {username}@{kernel_name}",
    "remote_ikernel_argv": [
        "/anaconda/envs/py35/bin/remote_ikernel",
        "manage",
        "--add",
        "--kernel_cmd=/anaconda/envs/py36/bin/ipython kernel -f {connection_file}",
        "--name=Remote VM",
        "--interface=ssh",
        "--host={username}@{ip}"
    ]
}

这应该与您在命令行中调用remote_ikernel相同:

remote_ikernel manage --add --kernel_cmd="/anaconda/envs/py36/bin/ipython kernel -f {connection_file}" --name="Remote VM" --interface=ssh --host={username}@{ip}

答案 1 :(得分:0)

不确定是否仍然有用,以防万一:
添加内核时,请使用--remote-precmd选项。
根据{{​​3}}:

  

在启动内核之前在远程主机上执行的命令,但是   转到工作目录后。

您可以使用此选项激活所需的环境:

--remote-precmd=source %PathToVenv%/Scripts/activate

我正在使用它,它可与remote_ikernel v0.4.6一起使用,而我的遥控器是Windows10。

对于Windows,该命令将如下所示:

--remote-precmd=%PathToVenv%\\Scripts\\activate.bat