# Difficulties setting up a kernel for jupyterlab in Windows

I recently downloaded SageMath for Windows and installed it. I have my habits in jupyterlab and wanted to add it as a new kernel, but encountered some difficulties.

### Locating the error

I followed instruction steps given at wiki.sagemath.org/SageWindows . When trying to start the kernel from a jupyterlab server I was met with an error message beginning with:

Failed to run command: ['/opt/sagemath-9.2/local/bin/sage', '--python', '-m', 'sage.repl.ipython_kernel', '-f', '[...]']

where [...] stands for the path to the kernel file. Considering that the shortcuts created at SageMath installation produced the expected result I compared it to the kernel specification and learned that this path specification is invalid in Windows and furthermore that the file trying to be run cannot be directly interpreted by Windows as an executable. The shortcuts work around it by running such commands in a mintty terminal.

### Trying to patch

Following this example I changed the kernel specifications with:"argv": ["C:/Program Files/Sagemath 9.2/runtime/bin/mintty","/opt/sagemath-9.2/local/bin/sage", "--python", "-m", "sage.repl.ipython_kernel", "-f", "{connection_file}"]. This was met with relative success as jupyter recognized this as a valid kernel specification, yet upon startup the kernel would not run, mintty windows would pop up and close while the jupyterlab server considered the kernel dead:

[I 2021-10-12 16:51:58.642 ServerApp] Kernel started: 54b584e9-bbb1-4f80-a69b-f42cab5ec102

[I 2021-10-12 16:52:01.638 ServerApp] AsyncIOLoopKernelRestarter: restarting kernel (1/5), new random ports

[I 2021-10-12 16:52:04.755 ServerApp] AsyncIOLoopKernelRestarter: restarting kernel (2/5), new random ports

[I 2021-10-12 16:52:07.806 ServerApp] AsyncIOLoopKernelRestarter: restarting kernel (3/5), new random ports

[I 2021-10-12 16:52:10.840 ServerApp] AsyncIOLoopKernelRestarter: restarting kernel (4/5), new random ports

[W 2021-10-12 16:52:13.892 ServerApp] AsyncIOLoopKernelRestarter: restart failed

[W 2021-10-12 16:52:13.892 ServerApp] Kernel 54b584e9-bbb1-4f80-a69b-f42cab5ec102 died, removing from map.

Trying to change the arguments with the configfiles for sage console, shell or notebook only changed evanescent mintty windows into as many mintty terminals running sage interpreter/shell/notebook server as attempts by jupyter to restart the kernel.

### Where help is welcome

I do not now if there is but seek a way to get the sage.repl.ipython_kernel running on its own and/or link it to jupyterlab to use it as a kernel for any notebook. The easiest for me would be to have everything as one Windows command to put into the "argv" key of the kernel specification. It would also be possible to include it in future builds of SageMath for Windows.

If impossible maybe there is a way to build a standalone executable to be linked in the kernelspec but I do not know the first thing about building this and linking it to the SageMath libs and resources.

Thanks in advance for any useful answer!

### Addendum

Reading mintty's manual I found I could use the option -h always to get the popping up windows to stay open. I am now able to read the error messages:

/usr/bin/env: 'bash': No such file or directory

C:/Program Files/SageMath 9.2/runtime/bin/bash: Exit 127.

Reading the sage files in the SAGE_ROOT and SAGE_LOCAL. My understanding is that the first one does some setting up and input sanitation before invoking the second one. This is where the code breaks with the shebang #!usr/bin/env bash. I tried having the root sage be launched through C:/Program Files/SageMath 9.2/runtime/bin/bash as the shortcuts do but it didn't change much. Yet when the shortcuts start sage there is no such error, which means that somehow mintty using /opt/sagemath-9.2/local/bin/sage under the -c option sets up bash in a path variable, at least form the local sage's point of view.

I do not understand how this happens and would like some help to recreate it.

edit retag close merge delete

## Comments

When sagemath starts a notebook server, it does so within cygwin (or WSL, depending how you run it). It then communicates with a kernel within the same environment. It sounds like you have a notebook server running fully natively on windows and you want it to start and communicate with a kernel within cygwin (or WSL). That means you need to get the appropriate inter-process-communication socket set up across an environment barrier. So, at least that the way sagemath itself does it probably needs to be adapted significantly for your situation. There are examples of "remote kernels" out there. That might help.

(getting a browser to connect to a server across an environment barrier is comparatively much easier, since the http protocol is already set up for that, hence sagemath's solution)

( 2021-10-17 21:20:16 +0100 )edit

In particular, I'd expect that starting the sage kernel through a terminal emulator won't allow your server to set up the right communication channels with the kernel process: terminal emulators are usually not set up to forward sockets in that way. Ssh can do that, and remote kernel solutions tend to tunnel all sorts of things through ssh connections. Usually these are set up for connections to different machines, so they may also work to bridge environment barriers on the same machine (and there may be solutions specific to cygwin that allow you to do this).

( 2021-10-17 21:25:07 +0100 )edit

Thank you very much! I think I'll come back to this from time to time with your suggested solutions until I get a good enough grasp on the architecture and actually solve it.

( 2021-11-12 01:58:50 +0100 )edit