Ask Your Question
3

Sagemath and Vscode

asked 2018-08-02 13:45:53 +0200

o6p gravatar image

Hi. I read this insightful post https://ask.sagemath.org/question/397... and I tried following the same steps for vscode, but it didn't seem to work. Has anyone managed to get Sage objects in vscode python editor?

edit retag flag offensive close merge delete

Comments

Highly doubtful. What exactly did you try and what exactly was the result?

Iguananaut gravatar imageIguananaut ( 2018-08-09 09:45:44 +0200 )edit

1 Answer

Sort by » oldest newest most voted
2

answered 2020-01-30 21:46:03 +0200

Dan-K gravatar image

updated 2020-01-31 21:22:07 +0200

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)

  • Common.1. Add the 'Python' VSCode extension and make sure it's enabled. This can be done by:

    • a. Pressing the extensions icon on the left icon bar within VSCode.
    • b. Typing in 'python' into the search bar.
    • c. Selecting the 'Python' extension by Microsoft.
    • d. Install or enable the extension.
  • Common.2. Pick any python environmnet recognised by VSCode.

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!

  1. Create or open a notebook.
  2. At the bottom right, select the python environment identified in step Common.2.
  3. Run a cell.
  4. Change the kernel at the top right to SageMath 0.9 for VSCode.
edit flag offensive delete link more

Comments

1

Well done. Shame about the patch to tornado; I don't see another way around it until/unless it's fixed upstream.

One thing I'm a bit confused by: You seem to mention that VSCode comes with its own Python into which you can install Jupyter Notebook, and indeed you mention pip installing Jupyter. But then you say you're running the Jupyter Notebook that comes with Sage, so the previous copy of notebook is never actually used as far as I can tell...unless you're just using Sage Notebook to start the kernel?

It would be easier if you copied the sage/kernel.json to somewhere else (not modifying the Sage installation, which is a bit dangerous), and instead installed a copy of that kernel directly into the jupyter notebook you installed on your system kernel. Should work.

Iguananaut gravatar imageIguananaut ( 2020-01-31 18:09:29 +0200 )edit

That's a good observation @Iguananaut! I didn't mean to imply VSCode has its own Python, but rather, it has a process for detecting installed Python environments. Unfortunately on Windows, it's not able to detect Sage's Python executable because VSCode uses "import sys; print(sys.executable)" to get the actual path, and since Sage's python is in a cygwin environment, it finds it under /usr/bin/python3 or something like that.

For Mac OSX (and probably Linux) you can totally do what you suggest, as Sage's Python can be detected as per step Common.2.

However VSCode needs a copy of Jupyter executables to function properly, and since it can't use Sage's ones on Windows, they need to be installed, even if we're connecting to an externally running Jupyter server.

Dan-K gravatar imageDan-K ( 2020-01-31 20:06:00 +0200 )edit

I'm not exactly sure what you mean here: "it's not able to detect Sage's Python executable because VSCode uses import sys; print(sys.executable) to get the actual path". In order to run some code in a specific Python interpreter, doesn't it need to already know the path to that Python interpreter in the first case? In what context does VSCode do this? I think I actually have VSCode installed but I never use it, or if not I'll go ahead and install it and see what I can find out...

Iguananaut gravatar imageIguananaut ( 2020-02-03 14:09:06 +0200 )edit

The Python log output in VSCode shows the process VSCode uses to find and validate each Python interpreter, including any one you explicitly set via "python.pythonPath" option. You'll find that it runs the command I mentioned and for Sage's Python the output is a POSIX path instead of a Windows one (since the executable runs under a cygwin environment).

Dan-K gravatar imageDan-K ( 2020-02-03 15:29:45 +0200 )edit

I don't think that's really the problem. I was able to set up a workspace where I set "python.pythonPath" to the windows path to Sage's Python interpreter. To get the Windows path, from the Sage shell you can run cygpath -m -a $(which python3), which in my case returns C:/Users/Erik M. Bray/AppData/Local/SageMath 9.0/runtime/opt/sagemath-9.0/local/bin/python3.7.exe. Putting this in "python.pythonPath"almost works, except the additional trick is that at the very least you also need to have the $PATH environment variable set properly. I was able to do this by creating a sage.env file like echo "$(cygpath -p -m "$PATH")" > sage.env, and then in my workspace settings set "python.envFile" to the path to that sage.env. This partly works....

Iguananaut gravatar imageIguananaut ( 2020-02-03 16:26:21 +0200 )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

2 followers

Stats

Asked: 2018-08-02 13:45:53 +0200

Seen: 19,646 times

Last updated: Jan 31 '20