Ask Your Question

Use PyCharm to edit and run Sage scripts

asked 2017-11-23 08:13:22 -0600

PHPirates gravatar image

updated 2017-11-24 05:23:37 -0600

How do I configure PyCharm so I can edit and run Sage scripts?

I am using SageMath 8 on Windows.

I thought I could maybe start PyCharm from the Sage shell, but after doing so I couldn't select Sage's python as interpreter, located at C:\Program Files\SageMath 8.0\runtime\opt\sagemath-8.0\local\bin\python.exe because it gave me Invalid Python SDK.

Above idea I got from 38750.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-11-23 14:16:16 -0600

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


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

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/", line 71, in <module>
  File "/usr/lib/python2.7/site-packages/sage/", 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()

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 )))

    print "%s is OK" % p

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


(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"


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 04:40:16 -0600 )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 15:41:27 -0600 )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 03:59:55 -0600 )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 04:04:44 -0600 )edit

Found out that in my C:/Program Files/SageMath 8.0/runtime/opt/sagemath-8.0/local/lib/python2.7/site-packages/sage/ 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 04:32:20 -0600 )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


Asked: 2017-11-23 08:08:17 -0600

Seen: 63 times

Last updated: Nov 24