Ask Your Question
1

Running Sage inside Python

asked 2016-01-09 18:25:28 +0100

giliev gravatar image

updated 2016-01-09 20:12:34 +0100

vdelecroix gravatar image

I was following a tutorial. I tried to run this code, but got sage is not recognized error. I am using Ubuntu (Mint). I added the sage root to my path. Now when I print $PATH in the terminal there is the sage path indeed. However, my script still does not recognize the Sage package. What may I be doing wrong? If any additional info is needed I will add it.

Here is the output of running $PATH:

bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/goran/Sage/sage-6.9-x86_64-Linux/sage

I am wondering what my header should look like (of the Python script). In the given example it is

#!/usr/bin/env sage.

I have been trying to adapt some other answers but none worked for me. What I want to achieve is calling Sage in Python code, because I have already written a couple of Python modules, and no I just need to call some Sage functions.

Thank you for the understanding!

edit retag flag offensive close merge delete

Comments

I upvoted your question and you might be able to post the link. If it is so, you can update your question.

vdelecroix gravatar imagevdelecroix ( 2016-01-09 19:47:35 +0100 )edit

What happens if you just type "sage" from the command line? Do you get Sage starting?

vdelecroix gravatar imagevdelecroix ( 2016-01-09 19:48:45 +0100 )edit

I updated the question. When I run "sudo sage" I start Sage and can run commands. I tried basic arithmetic and it works fine.

giliev gravatar imagegiliev ( 2016-01-09 19:55:26 +0100 )edit

Why do you need to do "sudo"? What about just "sage"?

vdelecroix gravatar imagevdelecroix ( 2016-01-09 20:02:13 +0100 )edit

What do you mean by "running $PATH"? It is a bash variable. There is no way to "run it".

vdelecroix gravatar imagevdelecroix ( 2016-01-09 20:12:58 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-01-09 20:11:02 +0100

vdelecroix gravatar image

updated 2016-01-09 20:56:27 +0100

You can not use the native python of your Ubuntu and call Sage functions. The native Python does not know anything about Sage functions. One possibility might be to use the Python from Sage. Just write a file "my_script" (no extension is needed) with

#!/complete/path/to/sage -python

import sage.all  # this is mandatory to initialize Sage

from sage.rings.arith import factor
print factor(18)

Then from the console do

$ ./my_script
2 * 3^2

Note that the magic of the #! just tells bash to use the Python from Sage. It has nothing to do with native Python versus Sage's Python.

extra remark: It seems not possible to use /usr/bin/env in conjunction with options...

edit flag offensive delete link more

Comments

My Ubuntu has Python 2.7, is that the issue? I saw somewhere that Sage uses Python 2.6. I have tried your suggestion, by writing .sage file and compiling it as sage "sage file_name.sage" and it works. But then I wanted to rename all my existing .py files into .sage files and I got issues with importing packages I have written. And if I cannot import my packages I do not see how can I separate my logic in separate files. I would like to keep writing native python code and only call Sage occasionally. I am working in PyCharm which gives me auto complete suggestions, which is of great help for writing Python code, but not Sage code.

Thank you for the suggestion!

giliev gravatar imagegiliev ( 2016-01-09 20:30:39 +0100 )edit

It is not an issue with Python version. Sage uses its own copy of python. You can get its version with

$ sage -python --version
Python 2.7.10

Note that you are not compiling anything here. The command "sage file_name.sage" just runs the code in "file_name.sage".

The header of the file is just here to get rid of "sage" in "sage file_name.sage". I updated my answer to make it clearer as apparently you are confusing many things.

vdelecroix gravatar imagevdelecroix ( 2016-01-09 20:36:15 +0100 )edit

OK, I am trying now to run it from PyCharm and to configure PyCharm to accept the Sage packets and not give me warnings. If you have any suggestion for good IDE they are very welcome :) However, this is out of the scope of this site and your answer explained me several misunderstandings. Thanks! :)

giliev gravatar imagegiliev ( 2016-01-09 22:44:17 +0100 )edit

@gilieve I'd recommend Jupyter.

A.Alharbi gravatar imageA.Alharbi ( 2016-01-10 15:53:04 +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

1 follower

Stats

Asked: 2016-01-09 18:25:28 +0100

Seen: 4,596 times

Last updated: Jan 09 '16