【问题标题】:Remote jupyter kernel - different virtual environment?远程jupyter内核 - 不同的虚拟环境?
【发布时间】:2019-08-28 03:43:05
【问题描述】:

我使用包 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": ""
}

【问题讨论】:

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


    【解决方案1】:

    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}
    

    【讨论】:

    • 嗯,我刚试过,但是当我执行import sys; print(sys.executable) 时,我仍然发现自己处于远程py35 环境中。
    • 稍微更新了我的问题:)
    【解决方案2】:

    不确定它是否仍然相关,但以防万一:
    添加内核时使用--remote-precmd 选项。
    根据docs:

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

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

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

    我正在使用它,它适用于 remote_ikernel v0.4.6,我的遥控器是 Windows 10。

    对于 Windows,命令应该是这样的:

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

    【讨论】:

    • 当您写“根据文档”时,最好将链接添加到包含您的报价的部分
    • 添加了项目主页的链接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 2016-10-19
    • 2018-07-06
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    相关资源
    最近更新 更多