Ask Your Question
3

Make PyCharm recognise the Sage python interpreter

asked 2017-11-23 15:13:22 +0100

PHPirates gravatar image

updated 2019-01-09 17:56:49 +0100

FrédéricC gravatar image

How do I configure PyCharm so I can edit and run Sage scripts? When I tried selecting Sage's python in C:\Program Files\SageMath 8.0\runtime\opt\sagemath-8.0\local\bin PyCharm said 'cannot setup SDK'.

I am using SageMath 8 on Windows.

[Edit] Rephrased the question because it is partly solved (and is pretty difficult), asking new question for the remainder so it is more clear.

edit retag flag offensive close merge delete

Comments

python.exe is just a symbolic link (in Cygwin, which is something native Windows software doesn't know about). For starters you'd have to link to the real executable (the one in the same path) named python2.7.exe. Beyond that I don't know what other problems you might encounter though.

Iguananaut gravatar imageIguananaut ( 2017-12-18 14:14:51 +0100 )edit

I'm downloading and installing PyCharm for myself to see how far I can get, but I suspect it's a wild goose chase.

Iguananaut gravatar imageIguananaut ( 2017-12-18 14:22:33 +0100 )edit

So far, I can at least get PyCharm somewhat working with the normal Cygwin Python interpreter (not Sage's yet). As I suspected it more or less works, though some configuration is necessary. I'll see if I can come up with some precise steps...

Iguananaut gravatar imageIguananaut ( 2017-12-18 14:48:51 +0100 )edit
1

Alright, I was able to get at least something vaguely working. I'll write up my findings in an answer below.

Iguananaut gravatar imageIguananaut ( 2017-12-18 15:25:40 +0100 )edit

3 Answers

Sort by » oldest newest most voted
8

answered 2017-12-18 16:49:14 +0100

Iguananaut gravatar image

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.

edit flag offensive delete link more

Comments

PyCharm now recognises the Sage python, thanks! Indeed, maybe this question should be about getting PyCharm to recognise the Sage python and the next question about the problems I encounter when actually trying to from sage.all import *.I will ask it soon!

PHPirates gravatar imagePHPirates ( 2017-12-20 08:44:16 +0100 )edit

Thanks! It worked for me, the only problem is that the debugger doesn't recognize the file. I get the error: "pydev debugger: warning: trying to add breakpoint to file that does not exist: .... (will have no effect)" while it doesn't appear when I use the regular interpreter. Can you please help me figure it out?

casper gravatar imagecasper ( 2020-04-09 15:18:57 +0100 )edit
2

answered 2017-11-23 21:16:16 +0100

dan_fulea gravatar image

This is a linux machine answering. The "answer" is possibly not the solution, but i need space to insert information, a comment is not enough.

In 38750 the procedure was described for a linux machine. sage -sh sets all needed variables for the operating system. In my case:

(sage-sh) dan@f... :~$ printenv | fgrep SAGE

gives the list of all more or less needed variables. Among them the most important one is

SAGE_ROOT=/usr

The right corresponding value(s) should maybe be set for Windows. Here is the list, well, i hesitated first to insert it, but soon we will see some of the variables again...

$ printenv | fgrep SAGE
SAGE_DOC_SRC=/usr/share/doc/sage
SAGE_ETC=/usr/etc
SAGE_SHARE=/usr/share
SAGE_NUM_THREADS_PARALLEL=4
SAGE_STARTUP_FILE=/home/dan/.sage//init.sage
SAGE_SPKG_INST=/usr/var/lib/sage/installed
SAGE_ORIG_PATH_SET=True
SAGE_SRC=/usr/share/sage/source
SAGE_DOC=/usr/share/doc/sage
SAGE_LOCAL=/usr
SAGE_ENV_SOURCED=4
SAGE_REPO_ANONYMOUS=git://trac.sagemath.org/sage.git
SAGE_DOC_MATHJAX=True
SAGE_ORIG_PATH=/home/dan/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
SAGE_LOGS=/usr/logs/pkgs
SAGE_EXTCODE=/usr/share/sage/ext
SAGE_ROOT=/usr
SAGE64=no
SAGE_DISTFILES=/usr/upstream
SAGE_NUM_THREADS=1
DOT_SAGE=/home/dan/.sage/
SAGE_REPO_AUTHENTICATED=ssh://git@trac.sagemath.org:2222/sage.git

A first try to set sage up and running in Windows is to insure that the Windows Python27 interpreter (that must be somewhere, possibly in C:\Python27) is found from the command line (running cmd.exe) and that inside of the python dialog box offered something like from sage.all import * is working...

Let me show the difference in Linux first. In a terminal...

$ python2.7
Python 2.7.14 (default, Sep 20 2017, 02:02:23) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from sage.all import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/sage/all.py", line 71, in <module>
    from sage.env import SAGE_ROOT, SAGE_SRC, SAGE_DOC_SRC, SAGE_LOCAL, DOT_SAGE, SAGE_ENV
  File "/usr/lib/python2.7/site-packages/sage/env.py", line 154, in <module>
    SINGULAR_SO = SAGE_LOCAL+"/lib/libSingular."+extension
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
>>>

Such information is hidden in the dark, when pycharm or an other IDE hits the issue.

The same after setting the right environment variables, using the simple sage -sh first:

$ sage -sh

Starting subshell with Sage environment variables set.  Don't forget
to exit when you are done.  Beware:
 * Do not do anything with other copies of Sage on your system.
 * Do not use this for installing Sage packages using "sage -i" or for
   running "make" at Sage's root directory.  These should be done
   outside the Sage shell.

Bypassing shell configuration files...

Note: SAGE_ROOT=/usr
(sage-sh) dan@f...:~$ python2.7
Python 2.7.14 (default, Sep 20 2017, 02:02:23) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from sage.all import *
>>> F = GF(2017)
>>> print F.multiplicative_generator()
5

My blind answer is to make the above first run from the command line in Windows. Then start pycharm against the right windows environment variables. In pycharm, or an other IDE, the python interpreter is still python27, not sage. To run sage inside, one has to do the above, possibly having to declare the libraries somewhere. In pycharm, this may be done using Settings > Project Structure . But my pycharm works without such a declaration.

Note that there is no pre-processing. So i have to type with contorsions something like...

from sage.all import *

for p in primes( 10**20, 10**20 + 1000 ):

    R = PolynomialRing( GF( p ), names='X' )
    X = R.gens()[0]

    if not (X**4 - 2).is_irreducible():
        print ("...reducible polynom mod %s :: X^4-2 = %s -> next prime..."
               % (p, factor( X**4 - 2 )))
        continue

    print "%s is OK" % p
    break

in order to get the first prime $p$ for which $X^4-2$ is irreducible in $\mathbb F_p[X]$. I could run the above code in my pycharm project.

Good luck!

edit flag offensive delete link more

Comments

(edited comment) Okay so I'm running now inside sage shell

SAGE_ROOT="C:/Program Files/SageMath 8.0/runtime/opt/sagemath-8.0"

sage -sh

cd "C:/Program Files/JetBrains/PyCharm 2017.2.3/bin"

./pycharm.exe

but in pycharm I get no module named sage.all. (I selected my system wide python2.7 as interpreter). That also happens when trying to import sage from python2 in the normal command prompt.

PHPirates gravatar imagePHPirates ( 2017-11-24 11:40:16 +0100 )edit

On this linux machine, sage-all is found here:

sage: print sage.all
<module 'sage.all' from '/usr/lib/python2.7/site-packages/sage/all.pyc'>

Is there any corresponding entry in the Windows site-packages installation?

Also, does sage -sh really set the SAGE* environment variables?! Please try

sage -sh
set SAGE

in the windows command line window ( alias cmd ) .

(I'm just trying to get closer to the source of the problem, which is still far away - i think.)

(A way to avoid all such problems is to install sage on linux and use it for mathematical / scientific production. This comment should please not be understood as an operating system discussion, it is only a comment about the way in which a system, a program supports a task, or only makes it possible.)

dan_fulea gravatar imagedan_fulea ( 2017-11-24 22:41:27 +0100 )edit

Oh thanks that is very interesting, because that means that you indeed managed to get the module installed in Python. The module is not in C:\Python27\Lib\site-packages for me (obviously) but only regular python packages. So I guess I need to figure out how to get the module there, and then PyCharm will be able to find it as a module. Any clue how you got it there?

Does, by any chance, your python2 site-packages/sage contain packages from algebras to typeset? I have those, and for every package an all.pyc as well, in the directory C:\Program Files\SageMath 8.0\runtime\opt\sagemath-8.0\local\lib\python2.7\site-packages\sage. I think this is the directory of Sage's python2?

After sage -sh then printenv SAGE_ROOT is correct, so that should be okay right?

PHPirates gravatar imagePHPirates ( 2017-11-25 10:59:55 +0100 )edit

I also tried setx PYTHONPATH "C:/Program Files/SageMath 8.0/runtime/opt/sagemath-8.0/local/lib/python2.7/site-packages/sage" /M (sets the system environment variable) in the hope that it would make python find the packages, but no luck.

A few years ago I managed to add a package (which included c libraries) to python and I think I did that by just pointing my PYTHONPATH to it, but can't remember exactly.

I know Sage is made for linux. But it's not for nothing that Sage was made available for Windows this summer. I think to make Sage more generally accepted it would help much if it would be available for Windows and it would have a proper IDE to work with. At least in my university it would, and I would very much like to spread the word of Sage, also to Windows!

PHPirates gravatar imagePHPirates ( 2017-11-25 11:04:44 +0100 )edit

Found out that in my C:/Program Files/SageMath 8.0/runtime/opt/sagemath-8.0/local/lib/python2.7/site-packages/sage/env.py on line 88 it makes use of os.uname, only available on unix systems. There must be much more of these things, so I do not think this will ever work on Windows?

Thanks for all the help, and who knows maybe this question will become relevant in the future again :)

PHPirates gravatar imagePHPirates ( 2017-11-25 11:32:20 +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

2 followers

Stats

Asked: 2017-11-23 15:08:17 +0100

Seen: 17,086 times

Last updated: Jan 09 '19