12

Is it possible to import the SageMath functions inside a python session?

What I wish to do, from a user perspective is something like this:

>>> import sage
>>> sage.kronecker_symbol(3,5)  # ...or any other sage root functions

instead of accessing kronecker_symbol(3,5) from a sagemath session. If possible, it would be very handy, as would allow embedding all the functionalities of SageMath within the python world.

kelalaka
  • 5,064
  • 5
  • 27
  • 44
SeF
  • 3,864
  • 2
  • 28
  • 41

3 Answers3

9

Importing SageMath functions in a Python session

There are several ways to achieve that.

SageMath from the operating system's package manager

Some operating systems have Sage packaged natively, for example Arch Linux, Debian, Fedora, Gentoo, NixOS, and their derivatives (Linux Mint, Manjaro, Ubuntu...).

See the dedicated "Distribution" page on the Sage wiki:

If using one of those, use the package manager to install sage or sagemath and then the Sage library will be installed on the system's Python, and in that Python it will become possible to do things like

>>> from sage.arith.misc import kronecker
>>> kronecker(3, 5)
-1

Another option is to use a cross-platform package manager such as Conda, Guix and Nix. These should work on most Linux distributions and macOS. Yet another option would be to run a Docker container.

I will detail the Conda case below.

SageMath with Conda

Install Sage with Conda and you will get that.

Instructions are here:

and start by installing a Conda distribution, either Miniconda, Minimamba or Anaconda, and then creating a sage conda environment.

Once a sage conda environment is installed, activate it:

conda activate sage

With that sage conda environment active, run

python

and importing the sage module or importing functions such as kronecker from that module should work.

Samuel Lelièvre
  • 3,212
  • 1
  • 14
  • 27
  • 1
    Great answer! Thanks! Is there a way to install it on the python interpreter in a virtualenvironment (and without using conda)? – SeF Apr 26 '20 at 11:02
  • 1
    @SeF Making the SageMath library a pip-installable package (depending on having installed all the required components) is the object of Sage Trac ticket 21507: https://trac.sagemath.org/ticket/21507 -- achieving that sooner might be possible depending on the operating system. – Samuel Lelièvre May 09 '20 at 23:48
  • Importing sage/sagemath into VS Code won't work. Any ideas? – Vaggelis Manousakis Oct 08 '20 at 14:41
  • 1
    @Evaggelos Manousakis you need to set up VS Code to use the same Python your Sage is based on. – Samuel Lelièvre Oct 09 '20 at 23:34
  • I have been trying for weeks to make it work but I didn't try this one. I will do my best and let you know. Thanks!!! – Vaggelis Manousakis Oct 14 '20 at 10:11
6

This is a complementary answer for those who fail to use some SageMath function that is not compatible with Python syntax.

For example;

from sage.all import *

F = GF(2)
R.<x> = k[]

This will give an error on R.<x> = k[] since it is not a valid Python syntax. So how to solve this issue?

SageMath parses the SageMath syntax then use Python. One needs to use preparse to see the actual command.

sage: preparse('R.<x> = k[]')
"R = k['x']; (x,) = R._first_ngens(1)"

So replace the line, done!

from sage.all import *

F = GF(2)
R = k['x']; (x,) = R._first_ngens(1)

Unfortunately one must use SageMath to identify these.

kelalaka
  • 5,064
  • 5
  • 27
  • 44
-5

1/ pip install sage
2/ from sage import *

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 21 '22 at 17:25
  • I'm not sure what I downloaded, but that doesn't look like sagemath. – TrebledJ Nov 11 '22 at 22:16