Ask Your Question
1

What packages in sage can I use in python?

asked 2016-11-07 12:08:01 +0100

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

As I know, there is not a good tutorial explainging how to import sage packages in python. When I list_packages of sage, it shows that numpy, scipy, python_igraph are installed. Then I want to import these packages in python with the following codes

from sage.all import *

I can use numpy, scipy while python_igraph does not work. Could anyone explain why numpy, scipy work while python_igraph does not? How to get python_igraph work?

And what packages in sage can I use in python? Is there any explicit tutorial?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2016-11-07 14:20:37 +0100

slelievre gravatar image

updated 2016-11-08 16:23:02 +0100

Note that Sage ships its own Python, which is distinct from your system-wide Python. The packages installed for Sage's Python have no influence on your system-wide Python. The packages installed for your system-wide Python have no influence on Sage's Python.

To start Sage's Python, run sage -python or sage -ipython in a terminal.

Sage's Python comes equipped with many packages (numpy, scipy, sympy, etc).

Some of them are imported by Sage at startup, some are not. For instance, even though they are standard packages, numpy and scipy are not imported by Sage at startup. To use them in Sage, you need an explicit import.

Optional packages (such as igraph) need to be installed first. After you have installed some new packages for Sage, restart Sage's Python and you can import them.

In the case of igraph and python_igraph, it seems that you need to install both, but to use them you only need to type import igraph.

Before:

$ sage -ipython
Python 2.7.10 (default, Oct 26 2016, 00:37:24) 
Type "copyright", "credits" or "license" for more information.

IPython 5.0.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import numpy

In [2]: import scipy

In [3]: import sage

In [4]: import igraph
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-5-8e950eb5d8d8> in <module>()
----> 1 import igraph

ImportError: No module named igraph

Installing:

$ sage -i igraph
...
[igraph-0.7.1] Finished installing igraph-0.7.1.spkg

real    3m23.309s
user    7m38.410s
sys 1m6.209s
Sage build/upgrade complete!
$ sage -i python_igraph
...
Sage build/upgrade complete!
$

After:

$ sage -ipython
Python 2.7.10 (default, Oct 26 2016, 00:37:24) 
Type "copyright", "credits" or "license" for more information.

IPython 5.0.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import igraph

In [2]:
edit flag offensive delete link more

Comments

Thanks for your reply! I tested after I installed python-igraph and igraph, they were still cannot be imported by python. Then I sage -f python2 and now I can import igraph in python. And I wonder that why the only tutorial teaches from sage.all import * but not directly import somepackage?

edenharder gravatar imageedenharder ( 2016-11-07 14:38:10 +0100 )edit

Please suggest any improvement to the tutorial. Could you specify which part of the tutorial should be improved?

slelievre gravatar imageslelievre ( 2016-11-08 10:03:55 +0100 )edit

Thanks! http://doc.sagemath.org/html/en/faq/faq-usage.html#how-do-i-import-sage-into-a-python-script (How do I import Sage into a Python script) told me that to use from sage.all import * to import sage packages. When I list the installed sage packages, there is igraph and python-igraph. But from sage.all import * will not import them into python. I'd like to know, what kind of packages can be imported when I use from sage.all import *.

edenharder gravatar imageedenharder ( 2016-11-08 12:51:06 +0100 )edit

I suggest you open a new ask-sage question asking "What does from sage.all import * do?".

In the text of the question, you might refer to the question you asked here (ask sage question 35464).

Don't check the "community wiki" checkbox: it prevents you from earning karma points from your questions.

slelievre gravatar imageslelievre ( 2016-11-08 16:14:03 +0100 )edit

done! https://ask.sagemath.org/question/35483/what-does-from-sageall-import-do/ (https://ask.sagemath.org/question/354...)

edenharder gravatar imageedenharder ( 2016-11-08 19:39:16 +0100 )edit
0

answered 2019-05-25 05:40:36 +0100

Re: igraph import error. I installed sage (sagemath) on Arch Linux,

[victoria@victoria ~]$ pacman -Ss sagemath | grep -i installed
    community/sage-data-elliptic_curves 0.8.1-1 [installed]
    community/sagemath 8.7-4 [installed]

[victoria@victoria ~]$ which sage
    /usr/bin/sage

In my Python 3.7 venv:

(/home/victoria/venv/py37) [victoria@victoria ~]$ sage
┌────────────────────────────────────────────────┐
│ SageMath version 8.7, Release Date: 2019-03-23 │
│ Using Python 2.7.16. Type "help()" for help.   │
└────────────────────────────────────────────────┘
  ## NOTE: even in py37 venv, using sage's py27

sage: import igraph
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-73ffca960e68> in <module>()
----> 1 import igraph

ImportError: No module named igraph
sage:                                                                                                                                       
Exiting Sage (CPU time 0m0.11s, Wall time 1m13.47s).

# ----------------------------------------
## Need system (Arch Linux) install of: python2-igraph

[victoria@victoria ~]$ pacman -Ss igraph | grepic python
    A program for computing automorphism groups of graphs and digraphs
community/python-igraph 0.7.1.post6-5 [installed]
    Python bindings for the igraph library
community/python2-igraph 0.7.1.post6-5
    Python 2 bindings for the igraph library

[victoria@victoria ~]$ sudo pacman -S python2-igraph
  ...
  (1/1) installing python2-igraph  [#################################] 100%

# ----------------------------------------

[victoria@victoria ~]$ sage

┌────────────────────────────────────────────────┐
│ SageMath version 8.7, Release Date: 2019-03-23 │
│ Using Python 2.7.16. Type "help()" for help.   │
└────────────────────────────────────────────────┘
sage: from sage import *
sage: import igraph
sage:
edit flag offensive delete link more

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-11-07 12:08:01 +0100

Seen: 1,177 times

Last updated: Nov 08 '16