Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The easiest way I found to go about this is to first set some permanent environment variables in your Windows environment. This can be done through a GUI in Windows, or via the cmd console. The console commands to set the necessary variables are:

setx Path "%Path%;C:\Program Files\SageMath 8.1\runtime\opt\sagemath-8.1\local\lib;C:\Program Files\SageMath 8.1\runtime\opt\sagemath-8.1\local\bin;C:\Program Files\SageMath 8.1\runtime\bin;C:\Program Files\SageMath 8.1\runtime\lib\lapack"
setx SAGE_ROOT /opt/sagemath-8.1
setx SAGE_LOCAL /opt/sagemath-8.1/local
setx DOT_SAGE /dot_sage

There are likely some others that should be set as well, but that was the bare minimum I found necessary to get Sage's Python interpreter working happily in PyCharm.

Then we need to correctly add Sage's Python interpreter to PyCharm's list of available interpreters. From the Welcome screen click on the little "Configure" gear and select "Settings" from the menu:

In the settings page select "Project Intepreter"--this is where you can add additional interpreters for use across projects. You might have something different shown here for an already existing interpreter.

Click the little gear icon in the top-right corner next to the drop-down list of available interpreters, and select "Add Local" from the menu. Then from the list of interpreter types, select "System Interpreter":

Browse to the python2.7.exe executable under C:\Program Files\SageMath 8.1\runtime\opt\sagemath-8.1\local\bin (or you can just copy this path directly into the file browser):

If you set up the correct environment variables, PyCharm should be able to successfully execute Sage's Python interpreter, and also populate the list of packages installed for that interpreter, and you'll see something like this:

If you didn't set the Path environment variable correctly, then you'll get an error here (it might show "Permission error" or something like that). This is because Windows uses the Path environment variable to search for DLLs, and the correct paths need to be added in order to find the Cygwin DLL, among others. Otherwise the interpreter executable can't even start. If this does happen you can still proceed with adding that interpreter, but it won't work until you set the necessary environment variables.

Then when you start a new project, make sure to select the Sage Python as the default interpreter (you might be able to use it with a virtualenv as well but I haven't tried that--if you do make sure it inherits system site-packages, or else you don't get Sage, etc.) In general I don't think it's necessary:

Finally, PyCharm also allows you to set environment variables to run the interpreter with on a per-project basis in a couple differentl places. You can configure environment variables in the "Run" configurations, and then again (separately, unfortunately) in the settings for the Console. This is actually what I did initially--I set Path, as well as SAGE_ROOT etc. just in PyCharm and that worked as well. But I found it simpler to just set the environment variables permanently in my environment. Then things "just worked".

Be aware that this is still just the normal Python interpreter, not the Sage interpreter, so it doesn't know how to run .sage files, nor is it aware of Sage syntax. But you should be able to from sage.all import * and use Sage objects in plain Python. To get that all working might be a little bit tricky, and I think should be tackled as a separate question.