Ask Your Question
1

What packages in sage can I use in python?

asked 2016-11-07 05:08:01 -0500

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

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-11-07 07:20:37 -0500

updated 2016-11-08 09:23:02 -0500

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 07:38:10 -0500 )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 03:03:55 -0500 )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 05:51:06 -0500 )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 09:14:03 -0500 )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 12:39:16 -0500 )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-11-07 05:08:01 -0500

Seen: 90 times

Last updated: Nov 08 '16