Ask Your Question

Run sage notebook without sage.all imported

asked 2019-09-09 12:25:11 +0200

Tilpo gravatar image

When I create a create a sage Jupyter notebook the kernel seems to always run from sage.all import *. This can be useful since you don't have to worry about importing all the things you need. However I find it has two big disadvantages:

  • The sage kernel takes a while to start (roughly one minute for me)
  • It causes naming conflicts when we define a function or class that already has a meaning somewhere in the huge sage library.

Therefore I wonder if it is possible to run a notebook with a sage kernel without importing the entire sage library.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2019-09-09 15:09:44 +0200

vdelecroix gravatar image

The fact that Sage takes so long to start is a problem. Though, if you want to use any function from Sage you need to run this import as it does initialize the many libraries it is using. You can still run a Python 2 (or Python 3 depending on your configuration) kernel and then run from sage.all import * in a cell. The kernel will start faster but you will still have to wait for the initialization to be done.

edit flag offensive delete link more


But my point is that I don't want to use from sage.all import *. I only need a couple libraries. If I try to import a sage library from a Python 2 kernel, I either get an ImportError or the kernel crashes. Is it really only possible to import the entire sage library or nothing at all?

Tilpo gravatar imageTilpo ( 2019-09-09 15:55:59 +0200 )edit

Short answer: no

Longer answer: it is complicated to make work together many libraries and the fact that Python is easily subject to cyclic imports. The initialization of the Sage module (in sage.all) is quite possibly not optimal as it does initialize and import everything. I agree with you that the initialization of the libraries should be made more local but it is currently not the case.

Advertisement: SageMath is a collaborative open source project. You are very welcome to propose concrete improvement by submitting code to the trac server.

vdelecroix gravatar imagevdelecroix ( 2019-09-09 16:58:10 +0200 )edit

I'm quite certain it's possible to speed of Sage initialization by importing just a smaller set of core modules, and I would like to be able to do that too. Even with the import cycles (some of which I believe can be broken with appropriate refactoring, although some others are unavoidable), I think it should be possible to get imports from sage working without importing as much as sage.all does. It's something I've always wanted to work on but it is a daunting task no doubt.

Iguananaut gravatar imageIguananaut ( 2019-09-09 17:49:23 +0200 )edit

That makes sense, thanks for clearing that up.

Tilpo gravatar imageTilpo ( 2019-09-09 20:13:30 +0200 )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


Asked: 2019-09-09 12:25:11 +0200

Seen: 222 times

Last updated: Sep 09 '19