问题
I use the package remote_ikernel to automatically connect to an azure VM from an azure VM. The two systems are identical, that is, they have the same python environments.
I start a remote kernel using
ssh -oStrictHostKeyChecking=no {username}@{ip} sudo /anaconda/envs/py36/bin/ipython kernel
And then update my local kernel.json by filling the parameters in the script below.
{{
"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}"
]
}}
First attempt
However, we don't want to use the remote /anaconda/envs/py35/bin/python but its py36 brother. I hoped to solve that by adjusting it in the 1st command, but that doesn't solve it. It also didn't help to change any of the py35 in the kernel.json. How can we achieve that?
Second attempt (edit)
I adapted the kernel.json to include the pointer to the py36 environment. See the updated version above. The log below looks fine, but import sys; sys.executable still shows the py35 version.
[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.
With the remote kernel file showing
{
"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": ""
}
回答1:
The kernel_cmd is what is run after making the remote connection, so to get the py36 version of ipython on the remote machine, your kernel.json should look more like:
{
"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}"
]
}
This should be the same as if you invoke remote_ikernel with the commandline:
remote_ikernel manage --add --kernel_cmd="/anaconda/envs/py36/bin/ipython kernel -f {connection_file}" --name="Remote VM" --interface=ssh --host={username}@{ip}
回答2:
Not sure if it's still relevant but just in case:
Use the --remote-precmd option when adding your kernel.
According to the docs:
Command to execute on the remote host before launching the kernel, but after changing to the working directory.
You can use this option to activate your needed env:
--remote-precmd=source %PathToVenv%/Scripts/activate
I'm using it and it works with remote_ikernel v0.4.6 and my remote is Windows 10.
For Windows, the command would be something like this:
--remote-precmd=%PathToVenv%\\Scripts\\activate.bat
来源:https://stackoverflow.com/questions/55730569/remote-jupyter-kernel-different-virtual-environment