Ask Your Question
1

Unable to link Jupyter notebook to correct kernel

asked 2021-09-17 01:35:47 +0100

Will Orrick gravatar image

updated 2024-08-26 11:18:14 +0100

FrédéricC gravatar image

A while ago I installed sage version 9.1 on my MacBook Pro running MacOS Mojave 10.14.6. That installation was from binary and was made in the MacOS Applications folder. Today I tried to install sage version 9.4, compiling from source. The install was in a subfolder of my user folder. The install seems fine, and I am able to run sage 9.4 from the command line.

Now I am trying to run 9.4 in a Jupyter notebook. After typing

./sage -n jupyter

or

./sage -n jupyterlab

in the root directory of my new installation things start well—the following banner comes up in the terminal,

┌────────────────────────────────────────────────┐
│ SageMath version 9.4, Release Date: 2021-08-22 │
│ Using Python 3.9.5. Type "help()" for help.    │
└────────────────────────────────────────────────┘
Please wait while the Jupyterlab server starts...
[I 18:26:42.688 LabApp] JupyterLab extension loaded from /Users/williamorrick2/sage- 9.4/local/lib/python3.9/site-packages/jupyterlab
[I 18:26:42.688 LabApp] JupyterLab application directory is /Users/williamorrick2/sage-9.4/local/share/jupyter/lab
[I 18:26:42.694 LabApp] Serving notebooks from local directory: /Users/williamorrick2/sage-9.4
[I 18:26:42.694 LabApp] Jupyter Notebook 6.1.1 is running at:
[I 18:26:42.694 LabApp] http://localhost:8888/?token=ba7dda9e8563ac920c9dd6ae2ab402980df04273ce90c616
[I 18:26:42.694 LabApp]  or http://127.0.0.1:8888/?token=ba7dda9e8563ac920c9dd6ae2ab402980df04273ce90c616
[I 18:26:42.694 LabApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 18:26:42.704 LabApp] 

    To access the notebook, open this file in a browser:
        file:///Users/williamorrick2/Library/Jupyter/runtime/nbserver-2218-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=ba7dda9e8563ac920c9dd6ae2ab402980df04273ce90c616
     or http://127.0.0.1:8888/?token=ba7dda9e8563ac920c9dd6ae2ab402980df04273ce90c616
[I 18:26:45.498 LabApp] Build is up to date
[I 18:26:45.750 LabApp] Kernel started: 8500f3e9-4730-4c6d-8363-5686721acc8e, name: sagemath

—but when the notebook opens (either in Jupyter notebook or in Jupyterlab) the kernel is sage 9.1 rather than 9.4. Furthermore I am unable to execute any code. In the terminal, immediately following the lines quoted above, I get a bunch of messages like

> /Applications/SageMath-9.1.app/Contents/Resources/sage/local/bin/sage: line 619: /Users/williamorrick2/sage-9.4/local/bin/python2: No such file or directory

which suggest that Jupyter is running the old kernel in the Applications folder, and that it is looking for python2 under my new installation, which only contains python3.

How do I go about correcting this?

edit retag flag offensive close merge delete

Comments

My problem has been solved--see my answer and the better answer of Masacroso--but one mystery remains, namely why my sage 9.1 kernel was non-functional. (The kernel died as soon as I tried to run any code in the notebook.) Presumably this is some kind of path problem: the sage 9.1 kernel was looking for things it needed in my user directory rather than the Applications folder and not finding them.

Will Orrick gravatar imageWill Orrick ( 2021-09-19 00:22:27 +0100 )edit

Thinking about this some more, perhaps it makes sense. I started up sage 9.4 in my local user directory, which then started up jupyter, which then attempted to use an older sage 9.1 kernel. It makes some sort of sense that sage 9.1 wouldn't know where to look for things.

Will Orrick gravatar imageWill Orrick ( 2021-09-19 04:03:15 +0100 )edit

3 Answers

Sort by » oldest newest most voted
2

answered 2021-09-18 00:57:01 +0100

Will Orrick gravatar image

updated 2021-09-21 05:10:36 +0100

The fix I found was to remove the folder sagemath from the following folder:

/Users/williamorrick2/Library/Jupyter/kernels

The kernel.json file in the sagemath folder had been directing Jupyter to the kernel of my old installation.

I found this idea at this StackOverflow post.

Added: Masacroso's answer provides, I think, the best general approach to such problems, something I would have learned if I'd delved into the other answers in the StackOverflow post linked above. The reason I'm accepting my own answer is that I think it may be useful to further MacOS users who visit this question to know the precise identity of the file causing the problem.

edit flag offensive delete link more

Comments

You can accept your own answer to mark the question as solved.

slelievre gravatar imageslelievre ( 2021-09-20 01:28:54 +0100 )edit
2

answered 2021-09-18 09:42:55 +0100

Masacroso gravatar image

Maybe this will help to someone. I dont have a Mac but in Ubuntu I do the following (and probably it works also in any other system): what I do is to link sagemath to a previous installation of jupyter in the system, to do this I just need first to delete any previous kernel of sagemath in Jupyter (if there is someone), running

jupyter kernelspec uninstall name-of-old-sagemath-kernel

in a terminal, but previously I run

jupyter kernelspec list

to know the kernels installed in jupyter. Once I have uninstalled from jupyter any old kernel of sagemath then I install the new one, running

jupyter kernelspec install --user /home/Masacroso/Documents/SageMath/local/share/jupyter/kernels/sagemath/

where I had sagemath unzipped in the folder /home/Masacroso/Documents/Sagemath. Then when I run jupyter lab from a terminal then one of the supported languages is sagemath.

edit flag offensive delete link more

Comments

Thanks. The install part wouldn't have been necessary in my case because the sage installation takes care of that. But "jupyter kernelspec list" would have alerted me to the copy of the sagemath kernel in /Users/williamorrick2/Library/Jupyter/kernels, which apparently takes precedence over the copy in the local sage directory, and which I could have then uninstalled. I guess uninstalling has the same effect as the deletion procedure I did?

Will Orrick gravatar imageWill Orrick ( 2021-09-18 16:40:23 +0100 )edit

@Will I suppose

Masacroso gravatar imageMasacroso ( 2021-09-18 17:48:43 +0100 )edit
1

answered 2021-09-17 07:24:31 +0100

slelievre gravatar image

updated 2021-09-20 01:26:46 +0100

Make sure sage points to the new Sage.

You could try which sage to check where the shell finds something called sage.

Then in case it is a symlink you could locate where it points to with

ls -halF $(which sage)

In case which sage returned /usr/local/bin/sage you can replace the symlink using

ln -sf /Users/williamorrick2/sage-9.4/sage /usr/local/bin

or, if that gives a permission error, the same with sudo:

sudo ln -sf /Users/williamorrick2/sage-9.4/sage /usr/local/bin
edit flag offensive delete link more

Comments

Thanks for your message. In my old installation I never ran sage from the command line--I had a sage icon in my dock and always started sage with that--and so may never have had occasion to create any symlinks; "which sage" returns nothing. I've added some relevant detail to my question, which, I think, shows that it really is sage 9.4 that is starting up. When I say I type "sage", I'm actually switching to the root directory of my new installation and typing "./sage".

Will Orrick gravatar imageWill Orrick ( 2021-09-17 12:51:54 +0100 )edit
1

If you would like to be able to start Sage from anywhere by typing sage, run this line:

ln -s /Users/williamorrick2/sage-9.4/sage /usr/local/bin

(or same with sudo if you get a permission error).

To undo:

rm /usr/local/bin/sage

(or same with sudo if this fails with a permission error).

slelievre gravatar imageslelievre ( 2021-09-20 01:28:05 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-09-17 01:35:47 +0100

Seen: 3,091 times

Last updated: Sep 21 '21