ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 09 Sep 2019 20:13:30 +0200Run sage notebook without sage.all importedhttps://ask.sagemath.org/question/47805/run-sage-notebook-without-sageall-imported/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.
Mon, 09 Sep 2019 12:25:11 +0200https://ask.sagemath.org/question/47805/run-sage-notebook-without-sageall-imported/Answer by vdelecroix for <p>When I create a create a sage Jupyter notebook the kernel seems to always run <code>from sage.all import *</code>.
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:</p>
<ul>
<li>The sage kernel takes a while to start (roughly one minute for me)</li>
<li>It causes naming conflicts when we define a function or class that already has a meaning somewhere in the huge sage library. </li>
</ul>
<p>Therefore I wonder if it is possible to run a notebook with a sage kernel without importing the entire sage library. </p>
https://ask.sagemath.org/question/47805/run-sage-notebook-without-sageall-imported/?answer=47806#post-id-47806The 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.Mon, 09 Sep 2019 15:09:44 +0200https://ask.sagemath.org/question/47805/run-sage-notebook-without-sageall-imported/?answer=47806#post-id-47806Comment by Tilpo for <p>The fact that Sage takes so long to start is a problem. Though, if you want to use <em>any</em> 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 <code>from sage.all import *</code> in a cell. The kernel will start faster but you will still have to wait for the initialization to be done.</p>
https://ask.sagemath.org/question/47805/run-sage-notebook-without-sageall-imported/?comment=47807#post-id-47807But 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?Mon, 09 Sep 2019 15:55:59 +0200https://ask.sagemath.org/question/47805/run-sage-notebook-without-sageall-imported/?comment=47807#post-id-47807Comment by vdelecroix for <p>The fact that Sage takes so long to start is a problem. Though, if you want to use <em>any</em> 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 <code>from sage.all import *</code> in a cell. The kernel will start faster but you will still have to wait for the initialization to be done.</p>
https://ask.sagemath.org/question/47805/run-sage-notebook-without-sageall-imported/?comment=47808#post-id-47808Short 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](https://trac.sagemath.org).Mon, 09 Sep 2019 16:58:10 +0200https://ask.sagemath.org/question/47805/run-sage-notebook-without-sageall-imported/?comment=47808#post-id-47808Comment by Tilpo for <p>The fact that Sage takes so long to start is a problem. Though, if you want to use <em>any</em> 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 <code>from sage.all import *</code> in a cell. The kernel will start faster but you will still have to wait for the initialization to be done.</p>
https://ask.sagemath.org/question/47805/run-sage-notebook-without-sageall-imported/?comment=47810#post-id-47810That makes sense, thanks for clearing that up.Mon, 09 Sep 2019 20:13:30 +0200https://ask.sagemath.org/question/47805/run-sage-notebook-without-sageall-imported/?comment=47810#post-id-47810Comment by Iguananaut for <p>The fact that Sage takes so long to start is a problem. Though, if you want to use <em>any</em> 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 <code>from sage.all import *</code> in a cell. The kernel will start faster but you will still have to wait for the initialization to be done.</p>
https://ask.sagemath.org/question/47805/run-sage-notebook-without-sageall-imported/?comment=47809#post-id-47809I'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.Mon, 09 Sep 2019 17:49:23 +0200https://ask.sagemath.org/question/47805/run-sage-notebook-without-sageall-imported/?comment=47809#post-id-47809