Ask Your Question

can't import sage into python

asked 2019-12-07 04:59:59 +0200

alienfetuseater gravatar image

updated 2019-12-07 14:20:22 +0200

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.")

from this sage docs link 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


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

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2019-12-07 17:31:34 +0200

The problem is that your script is called "", so when you try from sage.all import *, it tries to import it from this file. Rename your script to something else.

edit flag offensive delete link more


that 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'
alienfetuseater gravatar imagealienfetuseater ( 2019-12-08 03:01:56 +0200 )edit

Did you delete the old file ""?

John Palmieri gravatar imageJohn Palmieri ( 2019-12-08 03:15:11 +0200 )edit

i 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?

alienfetuseater gravatar imagealienfetuseater ( 2019-12-08 03:24:10 +0200 )edit

If 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).

John Palmieri gravatar imageJohn Palmieri ( 2019-12-08 07:35:03 +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

1 follower


Asked: 2019-12-07 04:59:59 +0200

Seen: 1,085 times

Last updated: Dec 07 '19