VSCode (1.41.1) doesn't officially support kernels other than Python (8521, 5078) but you can still get it to work by setting the kernel's "language"
to "python"
.
Here are steps to use Sage in VSCode assuming you have Sage and VSCode already installed.
Please follow both the common steps and the platform steps specific to your platform.
Common steps (Windows & Mac OSX)
It can be any environment you've configured for VSCode, or one automatically detected by VSCode.
Once you've picked the environment, take note of the paths to the python executable for that environment in order to execute the next 2 step's commands.
Note: There are some issues with VSCode which prevent you from choosing Sage's Python environment in Windows.
- Common.3. Install Jupyter Notebook for your selected environment.
Do this by running the following command:
/path/to/python -m pip install notebook
or:
/path/to/python -m pip install notebook --user
- Common.4. Install the default Python kernel.
This is required or else VSCode won’t work with Jupyter properly.
/path/to/python -m ipykernel install
or:
/path/to/python -m ipykernel install --user
Windows Steps (scroll down for Mac OSX)
The Windows solution involves launching the installed SageMath Notebook shortcut and connecting VSCode to this running instance via the python.dataScience.jupyterServerURI
VSCode setting.
- Windows.1. Upgrade
notebook
Python module.
This step was taken before the next one because it may have a fix for the next step, in which case the next step can be skipped.
path\to\python -m pip install notebook --upgrade
In the target python environment identified in step Common.2, modify this Python environment's Lib\site-packages\tornado\platform\asyncio.py
file (example: C:\Users\<User>\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\tornado\platform\asyncio.py
) and add the following right after import asyncio
:
import sys
if sys.platform == 'win32':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
Windows.3. Navigate to SageMath's Jupyter kernel directory which is typically C:\Users\<User>\AppData\Local\SageMath-9.0\runtime\opt\sagemath-9.0\local\share\jupyter\kernels
.
Windows.4. Duplicate the sagemath
folder and name it sagemath-vscode
.
Windows.5. Edit the new sagemath-vscode
folder's kernel.json
by changing the "language"
value to "python"
and change "display_name"
to "SageMath 9.0 for VSCode"
.
C:\Users\<User>\AppData\Local\SageMath-9.0\runtime\opt\sagemath-9.0\local\share\jupyter\kernels\sagemath-vscode\kernel.json
should look like this:
{
"argv": [
"/opt/sagemath-9.0/local/bin/sage",
"--python",
"-m",
"sage.repl.ipython_kernel",
"-f",
"{connection_file}"
],
"display_name": "SageMath 9.0 for VSCode",
"language": "python"
}
Windows.6. Launch the SageMath 9.0 Notebook
shortcut which was created by the SageMath installer.
Windows.7. Take note of the URL including the token value from the console output from the previous step.
Windows.8. Modify your VSCode's User or Workspace setting for python.dataScience.jupyterServerURI
to the URL identified in the previous step.
Note: For Windows, you need to update the python.dataScience.jupyterServerURI
everytime you re-run the SageMath 9.0 Notebook
shortcut.
Proceed to the last section!
Mac OSX Steps
MacOSX.1. Take note of the path to your SageMath's python executable.
This is typically found in the following folder /Applications/SageMath-9.0.app/Contents/Resources/sage/local/bin/python3
.
MacOSX.2. Locate your Jupyter kernel folder.
This can be seen from the output of step Common.4
and is typically ~/Library/Jupyter/kernels
.
MacOSX.3. Create a folder named sagemath-vscode
in the kernel folder identified in the previous step.
MacOSX.4. Create a file named kernel.json
in the sagemath-vscode
folder which was just created and populate it with the following content.
Make sure the python path you enter here is the same as the one identified in step MacOSX.1
:
{
"argv": [
"/Applications/SageMath-9.0.app/Contents/Resources/sage/local/bin/python3",
"-m",
"sage.repl.ipython_kernel",
"-f",
"{connection_file}"
],
"display_name": "SageMath 9.0 for VSCode",
"language": "python"
}
You can now use SageMath with VSCode!
- Create or open a notebook.
- At the bottom right, select the python environment identified in step
Common.2
. - Run a cell.
- Change the kernel at the top right to
SageMath 0.9 for VSCode
.
Highly doubtful. What exactly did you try and what exactly was the result?