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.Sun, 08 Dec 2019 07:35:03 +0100can't import sage into pythonhttps://ask.sagemath.org/question/48970/cant-import-sage-into-python/trying to write a python/sage script and in starting i am following the example
#!/usr/bin/env sage
import sys
from sage.all import *
if len(sys.argv) != 2:
print("Usage: %s <n>" % sys.argv[0])
print("Outputs the prime factorization of n.")
sys.exit(1)
print(factor(sage_eval(sys.argv[1])))
from [this sage docs link](https://doc.sagemath.org/html/en/tutorial/programming.html#standalone-python-sage-scripts) and i continue to get the variety of erros:
> ModuleNotFoundError: No module named 'sage.all'; 'sage' is not a package
> ImportError: No module named all
and variations thereof. sage runs from any directory just by typing sage and enter, so i dont believe its a path issue. ive also tried passing the -python argument to the shebang at the top of the script. i have tried to execute the script using:
> sage -python sage.py
> ./sage.py
i have also tried saving the file with a .sage extension and invoking the script also as above, all of and any combinations complain of the same inability to resolve the module importing of sage into the script. i know there are a number of similar questions on this site related to that error, but they most all seemed to also have problems invoking sage successfully from the cl with just typing sageSat, 07 Dec 2019 04:59:59 +0100https://ask.sagemath.org/question/48970/cant-import-sage-into-python/Answer by John Palmieri for <p>trying to write a python/sage script and in starting i am following the example</p>
<pre><code> #!/usr/bin/env sage
import sys
from sage.all import *
if len(sys.argv) != 2:
print("Usage: %s <n>" % sys.argv[0])
print("Outputs the prime factorization of n.")
sys.exit(1)
print(factor(sage_eval(sys.argv[1])))
</code></pre>
<p>from <a href="https://doc.sagemath.org/html/en/tutorial/programming.html#standalone-python-sage-scripts">this sage docs link</a> and i continue to get the variety of erros:</p>
<blockquote>
<p>ModuleNotFoundError: No module named 'sage.all'; 'sage' is not a package</p>
<p>ImportError: No module named all</p>
</blockquote>
<p>and variations thereof. sage runs from any directory just by typing sage and enter, so i dont believe its a path issue. ive also tried passing the -python argument to the shebang at the top of the script. i have tried to execute the script using:</p>
<blockquote>
<p>sage -python sage.py</p>
<p>./sage.py</p>
</blockquote>
<p>i have also tried saving the file with a .sage extension and invoking the script also as above, all of and any combinations complain of the same inability to resolve the module importing of sage into the script. i know there are a number of similar questions on this site related to that error, but they most all seemed to also have problems invoking sage successfully from the cl with just typing sage</p>
https://ask.sagemath.org/question/48970/cant-import-sage-into-python/?answer=48973#post-id-48973The problem is that your script is called "sage.py", so when you try `from sage.all import *`, it tries to import it from this file. Rename your script to something else.Sat, 07 Dec 2019 17:31:34 +0100https://ask.sagemath.org/question/48970/cant-import-sage-into-python/?answer=48973#post-id-48973Comment by alienfetuseater for <p>The problem is that your script is called "sage.py", so when you try <code>from sage.all import *</code>, it tries to import it from this file. Rename your script to something else.</p>
https://ask.sagemath.org/question/48970/cant-import-sage-into-python/?comment=48974#post-id-48974that worked, thank you very much. don't know if i should be surprised with that or not, not very familiar with python behavior. and so would you be able to comment why pylint is throwing so many errors?
No name 'all' in module 'sage'
Unable to import 'sage.all'
Undefined variable 'factor'
Undefined variable 'sage_eval'Sun, 08 Dec 2019 03:01:56 +0100https://ask.sagemath.org/question/48970/cant-import-sage-into-python/?comment=48974#post-id-48974Comment by John Palmieri for <p>The problem is that your script is called "sage.py", so when you try <code>from sage.all import *</code>, it tries to import it from this file. Rename your script to something else.</p>
https://ask.sagemath.org/question/48970/cant-import-sage-into-python/?comment=48975#post-id-48975Did you delete the old file "sage.py"?Sun, 08 Dec 2019 03:15:11 +0100https://ask.sagemath.org/question/48970/cant-import-sage-into-python/?comment=48975#post-id-48975Comment by alienfetuseater for <p>The problem is that your script is called "sage.py", so when you try <code>from sage.all import *</code>, it tries to import it from this file. Rename your script to something else.</p>
https://ask.sagemath.org/question/48970/cant-import-sage-into-python/?comment=48976#post-id-48976i did yes, even moved the files in question to a new folder. seems like pylint is having trouble identifying sage as a dependency of the script maybe?Sun, 08 Dec 2019 03:24:10 +0100https://ask.sagemath.org/question/48970/cant-import-sage-into-python/?comment=48976#post-id-48976Comment by John Palmieri for <p>The problem is that your script is called "sage.py", so when you try <code>from sage.all import *</code>, it tries to import it from this file. Rename your script to something else.</p>
https://ask.sagemath.org/question/48970/cant-import-sage-into-python/?comment=48978#post-id-48978If pylint is using the system's Python, that could explain it, since Sage is (typically) not installed as a module for the system's Python, only Sage's own Python installation. You could try installing pylint in Sage (`sage --pip install pylint`, I think).Sun, 08 Dec 2019 07:35:03 +0100https://ask.sagemath.org/question/48970/cant-import-sage-into-python/?comment=48978#post-id-48978