Running Sage from an IDE like PyCharm on Windows.

1134 views
Skip to first unread message

PHPirate

unread,
Dec 15, 2017, 12:23:51 PM12/15/17
to sage-devel
I already tried asking at SO and ask.sagemath.org but I couldn't reach any Windows users of Sage, then I tried at the GitHub Windows release repo with results of the previous questions and there someone referred me here so here is the copy of my question:

My question: 
In order to make Sage really a success for me I want a proper IDE. I want to be able to edit and run Sage scripts with PyCharm. How do I do this on Windows?

I think there exist people running this setup on Linux, so I think it should be possible on Windows as well. 

My attempt currently consists of running inside the 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


In the PyCharm project I selected my system wide python2.7 as interpreter, but I get `no module named sage.all` which makes sense because I didn't install the module into my system wide python.

After `sage -sh` I checked with `printenv SAGE_ROOT` that the environment variable was set correctly.

I did figure out that Sage's `sage.all` module is installed in Sage's python, at `C:\Program Files\SageMath 8.0\runtime\opt\sagemath-8.0\local\lib\python2.7\site-packages\sage`. But PyCharm does not want to recognise Sage's python as a valid interpreter.

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,

I also figured that it didn't make sense to just copy the sage module into my system wide python installation, because the module makes use of for example `os.uname` which is not available on Windows. That said, I have no clue at all how the Sage console even works :) 

PS Actually this is the only thing withholding me from spreading Sage in my university, since the console and SageTeX work fine - although the console takes 45 secs up to a minute to start and SageTeX instructions are not made for windows.
PPS Glad to see that since I installed Sage from here the number of stars went from 2 to 16, keep it going :) 
PPPS Just saw you have released Sage 8.1 an hour ago, not sure if that affects anything. Will update Sage later.

Dima Pasechnik

unread,
Dec 15, 2017, 1:20:54 PM12/15/17
to sage-devel
How about using Jupyter as an IDE?
PyCharm is probably wedded to Windows too much to be useful in such a setup.

PHPirate

unread,
Dec 16, 2017, 7:19:56 AM12/16/17
to sage-devel
Thanks, it sounds reasonable. But do you mean the Jupyter notebook included with Sage, which you can start with
sage --notebook ipython
from the Sage shell? I do not like notebooks such as this one and Mathematica because they do not go well with a VCS. Is it then possible to use this Jupyter to edit and run Sage files saved in a better way, like python files?

Dima Pasechnik

unread,
Dec 16, 2017, 8:01:08 AM12/16/17
to sage-devel
I've already expalined here https://github.com/sagemath/sage-windows/issues/12 that PyCharm doesn't support Cygwin Python,
and thus it's not going to be trivial to fix. The reason that we must use Cygwin Python is that a number of essential Sage components (i.e. Python extensions you need) e.g. GAP, won't work natively on Windows, as they use fork() and other Unix/Posix specific system functions.  

PHPirate

unread,
Dec 16, 2017, 8:15:00 AM12/16/17
to sage-devel
Aha, yes indeed I noticed that some Sage components will not work on Windows but I didn't know about the Cygwin Python. Thanks for explaining!

Erik Bray

unread,
Dec 18, 2017, 8:06:09 AM12/18/17
to sage-devel
That sounds a bit bogus to me.  I've never used PyCharm before and don't know how it works, but I suspect it could be made to work with Cygwin's Python.  It's pretty low-priority for me though.  I don't see how using PyCharm to edit sage source code would be useful--it won't even do syntax highlighting properly, unless I'm missing something.


On Saturday, December 16, 2017 at 2:01:08 PM UTC+1, Dima Pasechnik wrote:

Dima Pasechnik

unread,
Dec 18, 2017, 11:17:07 AM12/18/17
to sage-devel


On Monday, December 18, 2017 at 1:06:09 PM UTC, Erik Bray wrote:
That sounds a bit bogus to me.  I've never used PyCharm before and don't know how it works, but I suspect it could be made to work with Cygwin's Python. 

PyCharm is a closed-source product that has a wizard to pick up "the Python", and this wizard won't work with Cygwin's Python, as the product's vendor says. Bogus or not...

PHPirate

unread,
Dec 18, 2017, 11:37:21 AM12/18/17
to sage-devel
Hm, it is at least worth a try (just saw your message on GH) Okay I can understand if Sage has no syntax highlighting in any IDE on Windows, but as the situation is now for me, is that there is no IDE in which you can type Sage and then hit 'run' and then get Sage output. Now I think I could write Sage in Notepad and then execute a Sage file via the Sage shell but I'm looking to shortcut that a bit (my expectations are quite lower now I know that Sage doesn't have a standard editor which everyone uses).

But is it a bad idea to write Sage scripts? Did I misunderstand something, and should I use the console only?

Erik Bray

unread,
Dec 18, 2017, 11:38:56 AM12/18/17
to sage-devel
On Mon, Dec 18, 2017 at 5:17 PM, Dima Pasechnik <dim...@gmail.com> wrote:
>
>
> On Monday, December 18, 2017 at 1:06:09 PM UTC, Erik Bray wrote:
>>
>> That sounds a bit bogus to me. I've never used PyCharm before and don't
>> know how it works, but I suspect it could be made to work with Cygwin's
>> Python.
>
>
> PyCharm is a closed-source product that has a wizard to pick up "the
> Python", and this wizard won't work with Cygwin's Python, as the product's
> vendor says. Bogus or not...

I've confirmed that Cygwin's Python interpreter works fine with
PyCharm as long as the Path environment variable is set correctly.

So I think a more accurate interpretation of their statement is "We
don't know whether or not it will work and won't or can't comment on
if it will, nor provide support for it". But it does work.

>> It's pretty low-priority for me though. I don't see how using PyCharm to
>> edit sage source code would be useful--it won't even do syntax highlighting
>> properly, unless I'm missing something.
>>
>> On Saturday, December 16, 2017 at 2:01:08 PM UTC+1, Dima Pasechnik wrote:
>>>
>>> I've already expalined here
>>> https://github.com/sagemath/sage-windows/issues/12 that PyCharm doesn't
>>> support Cygwin Python,
>>> and thus it's not going to be trivial to fix. The reason that we must use
>>> Cygwin Python is that a number of essential Sage components (i.e. Python
>>> extensions you need) e.g. GAP, won't work natively on Windows, as they use
>>> fork() and other Unix/Posix specific system functions.
>>>
>>> On Saturday, December 16, 2017 at 12:19:56 PM UTC, PHPirate wrote:
>>>>
>>>> Thanks, it sounds reasonable. But do you mean the Jupyter notebook
>>>> included with Sage, which you can start with
>>>> sage --notebook ipython
>>>> from the Sage shell? I do not like notebooks such as this one and
>>>> Mathematica because they do not go well with a VCS. Is it then possible to
>>>> use this Jupyter to edit and run Sage files saved in a better way, like
>>>> python files?
>
> --
> You received this message because you are subscribed to the Google Groups
> "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sage-devel+...@googlegroups.com.
> To post to this group, send email to sage-...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sage-devel.
> For more options, visit https://groups.google.com/d/optout.

Erik Bray

unread,
Dec 18, 2017, 11:42:50 AM12/18/17
to sage-devel
On Mon, Dec 18, 2017 at 5:37 PM, PHPirate <holland...@gmail.com> wrote:
> Hm, it is at least worth a try (just saw your message on GH) Okay I can
> understand if Sage has no syntax highlighting in any IDE on Windows, but as
> the situation is now for me, is that there is no IDE in which you can type
> Sage and then hit 'run' and then get Sage output. Now I think I could write
> Sage in Notepad and then execute a Sage file via the Sage shell but I'm
> looking to shortcut that a bit (my expectations are quite lower now I know
> that Sage doesn't have a standard editor which everyone uses).
>
> But is it a bad idea to write Sage scripts? Did I misunderstand something,
> and should I use the console only?

It's not at all a bad idea; it's just that if you want correct syntax
highlighting for it you'll have to use an editor for which there is
syntax highlighting support for Sage, or add it yourself to your
editor of choice. Certainly there's no reason to use notepad
regardless. It's just that different editors have different means of
providing syntax highlighting for new languages (where Sage's syntax
is just a small superset over pure Python syntax).

More importantly, the default Python interpreter also isn't going to
know how to execute a Sage script, though it seems that in PyCharm
it's probably possible to configure the necessary options to pre-load
the Sage syntax parser and then pass it a .sage script, but I haven't
tried it yet.

> On Monday, December 18, 2017 at 2:06:09 PM UTC+1, Erik Bray wrote:
>>
>> That sounds a bit bogus to me. I've never used PyCharm before and don't
>> know how it works, but I suspect it could be made to work with Cygwin's
>> Python. It's pretty low-priority for me though. I don't see how using
>> PyCharm to edit sage source code would be useful--it won't even do syntax
>> highlighting properly, unless I'm missing something.
>>
>> On Saturday, December 16, 2017 at 2:01:08 PM UTC+1, Dima Pasechnik wrote:
>>>
>>> I've already expalined here
>>> https://github.com/sagemath/sage-windows/issues/12 that PyCharm doesn't
>>> support Cygwin Python,
>>> and thus it's not going to be trivial to fix. The reason that we must use
>>> Cygwin Python is that a number of essential Sage components (i.e. Python
>>> extensions you need) e.g. GAP, won't work natively on Windows, as they use
>>> fork() and other Unix/Posix specific system functions.
>>>
>>> On Saturday, December 16, 2017 at 12:19:56 PM UTC, PHPirate wrote:
>>>>
>>>> Thanks, it sounds reasonable. But do you mean the Jupyter notebook
>>>> included with Sage, which you can start with
>>>> sage --notebook ipython
>>>> from the Sage shell? I do not like notebooks such as this one and
>>>> Mathematica because they do not go well with a VCS. Is it then possible to
>>>> use this Jupyter to edit and run Sage files saved in a better way, like
>>>> python files?
>

PHPirate

unread,
Dec 18, 2017, 11:55:08 AM12/18/17
to sage-devel
Thanks, that sounds a bit too difficult for me though so I'll just stick to writing in PyCharm and try to execute my Sage files via the Sage shell.

But out of curiosity, am I the only one wanting to write scripts in Sage? Or are there other people using editors in the same way?

Peter Luschny

unread,
Dec 18, 2017, 12:40:58 PM12/18/17
to sage-devel
I do not like notebooks such as this one and Mathematica because they do not go well with a VCS. 

With Eclipse you have a powerful IDE for Mathematica. Called Wolfram Workbench. 

PHPirate

unread,
Dec 18, 2017, 2:24:57 PM12/18/17
to sage-devel
Bit off-topic but yeah, that's the kind of IDE Sage needs :) 
(MMa was maybe not the best example, I use packages all the time if I need to collaborate)

David Roe

unread,
Dec 18, 2017, 3:20:18 PM12/18/17
to sage-devel
On Mon, Dec 18, 2017 at 8:55 AM, PHPirate <holland...@gmail.com> wrote:
Thanks, that sounds a bit too difficult for me though so I'll just stick to writing in PyCharm and try to execute my Sage files via the Sage shell.

But out of curiosity, am I the only one wanting to write scripts in Sage? Or are there other people using editors in the same way?

I think many people write scripts for Sage, though much of that development isn't happening on Windows.  Personally, I use emacs on OS X.
David
 
To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+unsubscribe@googlegroups.com.

Dima Pasechnik

unread,
Dec 18, 2017, 4:25:14 PM12/18/17
to sage-devel


On Monday, December 18, 2017 at 8:20:18 PM UTC, David Roe wrote:


On Mon, Dec 18, 2017 at 8:55 AM, PHPirate <holland...@gmail.com> wrote:
Thanks, that sounds a bit too difficult for me though so I'll just stick to writing in PyCharm and try to execute my Sage files via the Sage shell.

But out of curiosity, am I the only one wanting to write scripts in Sage? Or are there other people using editors in the same way?

I think many people write scripts for Sage, though much of that development isn't happening on Windows.  Personally, I use emacs on OS X.

I use vim on Linux (as well as, if needed, on OSX and on FreeBSD). Surely it does syntax highlighting for Python/Cython
and with a small effort for Sage (as it's basically Python, language-wise) too...

I know people using atom for the same purpose (and yes, emacs for sure).

Vim and emacs run, natively, on Windows. IMHO Notepad is the last resort...

PHPirate

unread,
Dec 19, 2017, 1:30:42 AM12/19/17
to sage-devel
Thanks for the replies, so I would be very interested to run vim or emacs on Windows (although I have never heard of any windows user doing so) but since you both run a different OS you may not know how to set this up on Windows?
I'll try in any case later on, I think I should open a separate topic for that I guess? 

For now, I found some tips at https://wiki.sagemath.org/Tips about usage with vim, and a couple of vim plugins like https://github.com/petRUShka/vim-sage but no complete installation guide. Is there one? If not, if I manage to get it work I will write one myself then. 
I really hope I will manage to run Sage scripts from within vim, as the wiki hints is possible!

In any case, conclusion for this topic: don't use PyCharm, try vim (or if you want, emacs or atom or whatever) instead.

Let me quote Martin Vahi...
"As a side note I say that I've noticed that software developers, me myself included, are usually not as good at math as they _should_ and pure mathematicians tend to be at software development not as good as they could. That seems to explain a lot of things in this world. :-D"

Dima Pasechnik

unread,
Dec 19, 2017, 3:41:14 AM12/19/17
to sage-devel


On Tuesday, December 19, 2017 at 6:30:42 AM UTC, PHPirate wrote:
Thanks for the replies, so I would be very interested to run vim or emacs on Windows (although I have never heard of any windows user doing so) but since you both run a different OS you may not know how to set this up on Windows?

Internet is awash with instructions on installing emacs/xemacs and vim/gvim on Windows.
They are also available as a part of Cygwin and msys2.


 
I'll try in any case later on, I think I should open a separate topic for that I guess? 

For now, I found some tips at https://wiki.sagemath.org/Tips about usage with vim, and a couple of vim plugins like https://github.com/petRUShka/vim-sage but no complete installation guide. Is there one? If not, if I manage to get it work I will write one myself then. 
I really hope I will manage to run Sage scripts from within vim, as the wiki hints is possible!

In any case, conclusion for this topic: don't use PyCharm, try vim (or if you want, emacs or atom or whatever) instead.

IMHO Erik wrote that it appears to be possible to use PyCharm with Cygwin Python.
Thus it should be possible with Sage's Python too...

Erik Bray

unread,
Dec 19, 2017, 6:28:52 AM12/19/17
to sage-devel
On Tue, Dec 19, 2017 at 7:30 AM, PHPirate <holland...@gmail.com> wrote:
> Thanks for the replies, so I would be very interested to run vim or emacs on
> Windows (although I have never heard of any windows user doing so) but since
> you both run a different OS you may not know how to set this up on Windows?
> I'll try in any case later on, I think I should open a separate topic for
> that I guess?
>
> For now, I found some tips at https://wiki.sagemath.org/Tips about usage
> with vim, and a couple of vim plugins like
> https://github.com/petRUShka/vim-sage but no complete installation guide. Is
> there one? If not, if I manage to get it work I will write one myself then.
> I really hope I will manage to run Sage scripts from within vim, as the wiki
> hints is possible!
>
> In any case, conclusion for this topic: don't use PyCharm, try vim (or if
> you want, emacs or atom or whatever) instead.
>
> Let me quote Martin Vahi...
> "As a side note I say that I've noticed that software developers, me myself
> included, are usually not as good at math as they _should_ and pure
> mathematicians tend to be at software development not as good as they could.
> That seems to explain a lot of things in this world. :-D"

I should maybe help clarify for you--if all you care about is
*editing* files you don't need a full IDE, you just need an editor.
All an editor does is edit files (though most advanced editors can do
much more, with the proper extensions, such as run code or arbitrary
shell commands). An IDE contains an editor as a central component,
but it also has other development tools built into it like a compiler
front-end, front-ends for build tools, debuggers, etc. (hence
"integrated") rather than running an editor and those other tools as
separate components.

I wouldn't say one way of working is better or worse than another--it
depends in part on personal preferences and how you think, as well as
the type of project. I'd be surprised if any Sage developer uses a
full IDE for anything except maybe if they happen to like their IDE's
editor. This is in part because Sage has so many idiosyncrasies that
a traditional IDE probably won't work too well with it without
significant tweaking as you've found with trying to use it with
PyCharm.

I've personally never used an IDE for Python at all and I don't find
it that useful, but do use an IDE (Eclipse) when working on Java, and
Visual Studio when working on Windows-specific projects.

If you like PyCharm for its editor you can certainly get it working (I
provided instructions on the ask.sage question) to run code as well.
It should be straightforward to set up PyCharm to parse .sage scripts
through the sage-parser before running them with the plain Python
interpreter too. But you might also be just as well off finding a
stand-alone editor that you like.

Erik Bray

unread,
Dec 19, 2017, 6:29:47 AM12/19/17
to sage-devel
On Tue, Dec 19, 2017 at 9:41 AM, Dima Pasechnik <dim...@gmail.com> wrote:
>
>
> On Tuesday, December 19, 2017 at 6:30:42 AM UTC, PHPirate wrote:
>>
>> Thanks for the replies, so I would be very interested to run vim or emacs
>> on Windows (although I have never heard of any windows user doing so) but
>> since you both run a different OS you may not know how to set this up on
>> Windows?
>
>
> Internet is awash with instructions on installing emacs/xemacs and vim/gvim
> on Windows.
> They are also available as a part of Cygwin and msys2.
>
>
>
>>
>> I'll try in any case later on, I think I should open a separate topic for
>> that I guess?
>>
>> For now, I found some tips at https://wiki.sagemath.org/Tips about usage
>> with vim, and a couple of vim plugins like
>> https://github.com/petRUShka/vim-sage but no complete installation guide. Is
>> there one? If not, if I manage to get it work I will write one myself then.
>> I really hope I will manage to run Sage scripts from within vim, as the
>> wiki hints is possible!
>>
>> In any case, conclusion for this topic: don't use PyCharm, try vim (or if
>> you want, emacs or atom or whatever) instead.
>
>
> IMHO Erik wrote that it appears to be possible to use PyCharm with Cygwin
> Python.
> Thus it should be possible with Sage's Python too...

My instructions were actually specifically for Sage's Python (though
they would apply to a plain Cygwin Python as well).

PHPirate

unread,
Dec 19, 2017, 12:33:03 PM12/19/17
to sage-devel
Ho, I had not seen that you had posted on ask.sagemath.org! I don't have time to try right now, but I will later - I think it really helps a lot!

Re the question of IDE/editor: You're right I messed my wording up a bit, sorry. But I purposefully specified 'IDE' in the initial question, but when I found out that probably was not going to work (as I then thought) I put my hopes on making an editor able to execute Sage from within the editor - so make it a tiny bit integrated but not really. Thanks for writing it down clearly!
I find it very interesting to hear that you do not think PyCharm useful for writing in Python - but I use PyCharm nearly every day just as quite a lot of people, hence it would be very convenient if I could write sage in PyCharm - even without syntax highlighting or autocomplete.

PHPirate

unread,
Dec 20, 2017, 2:59:12 AM12/20/17
to sage-devel
Thanks to Erik Bray PyCharm does now successfully (see ask.sagemath.org) recognise Sage's python. Also in the packages list Sage shows.

When I now try to
from sage.all import *
 I get 
RuntimeError: libSingular not found--a working Singular install in $SAGE_LOCAL is required for Sage to work

ideas?

Dima Pasechnik

unread,
Dec 20, 2017, 5:53:15 AM12/20/17
to sage-devel
add the location of that .so file---somewhere in local/ subdirectory, as in setx Path command given by Erik in ask.sagemath.org---to Path in that command.

PHPirate

unread,
Dec 20, 2017, 6:01:26 AM12/20/17
to sage-devel
Hm, the only .so files I have in local/ are libzn_poly.so and libzn_poly-0.9.so
I do have Singular folders in local/share/singular and also in local/lib/python2.7/site-packages/sage/libs/singular
I added both of them to my path, but that didn't help.

Dima Pasechnik

unread,
Dec 20, 2017, 6:24:01 AM12/20/17
to sage-devel
there should be libSingular.so or/and libSingular.dll somewhere. Where? Find out and add these locations.

PHPirate

unread,
Dec 20, 2017, 6:43:11 AM12/20/17
to sage-devel
Ah, searched for dll and found in local/lib libSingular.a and libSingular.dll.a but adding local/lib to my path didn't help.

Dima Pasechnik

unread,
Dec 20, 2017, 6:53:46 AM12/20/17
to sage-devel
I don't think files with names ending with .a are the ones you need.
They are not DLLs.

Erik Bray

unread,
Dec 21, 2017, 4:53:08 AM12/21/17
to sage-devel
On Tue, Dec 19, 2017 at 6:33 PM, PHPirate <holland...@gmail.com> wrote:
> Ho, I had not seen that you had posted on ask.sagemath.org! I don't have
> time to try right now, but I will later - I think it really helps a lot!
>
> Re the question of IDE/editor: You're right I messed my wording up a bit,
> sorry. But I purposefully specified 'IDE' in the initial question, but when
> I found out that probably was not going to work (as I then thought) I put my
> hopes on making an editor able to execute Sage from within the editor - so
> make it a tiny bit integrated but not really. Thanks for writing it down
> clearly!
> I find it very interesting to hear that you do not think PyCharm useful for
> writing in Python - but I use PyCharm nearly every day just as quite a lot
> of people, hence it would be very convenient if I could write sage in
> PyCharm - even without syntax highlighting or autocomplete.

To be clear--I did not not say it's "not useful". I just personally
don't find IDEs that useful for Python. If it's useful to you then by
all means! And it would be nice if it worked better for Sage too.

Erik Bray

unread,
Dec 21, 2017, 4:54:26 AM12/21/17
to sage-devel
On Wed, Dec 20, 2017 at 8:59 AM, PHPirate <holland...@gmail.com> wrote:
> Thanks to Erik Bray PyCharm does now successfully (see ask.sagemath.org)
> recognise Sage's python. Also in the packages list Sage shows.
>
> When I now try to
> from sage.all import *
> I get
> RuntimeError: libSingular not found--a working Singular install in
> $SAGE_LOCAL is required for Sage to work
>
> ideas?

I'd have to see exactly what environment variables you set. When I
did this, according to the instructions I gave, I did not have this
problem.

PHPirate

unread,
Dec 21, 2017, 5:55:18 AM12/21/17
to sage-devel
Thanks, you were right! I updated to 8.1 and did everything again, and it works now! Not sure what the problem was.
It's not too fast (30 seconds to execute) but it works. But I still have to execute in the Sage shell
sage-preparse filename.sage
in order to generate a python file with the proper syntax.

I'm now trying to figure out how to run a Sage shell command from something like a batch file which I hope PyCharm could maybe execute, but I'm not sure if this is the way to go. As you said, this is a whole new problem.

At least when it would work, then I would write sage in a filename.sage file in PyCharm, then run and see the output within PyCharm. Would be awesome!
Reply all
Reply to author
Forward
0 new messages