Ask Your Question
0

can't import sage into python

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

alienfetuseater gravatar image

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

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 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 sage

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
5

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

The 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.

edit flag offensive delete link more

Comments

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 +0100 )edit

Did you delete the old file "sage.py"?

John Palmieri gravatar imageJohn Palmieri ( 2019-12-08 03:15:11 +0100 )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 +0100 )edit
1

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 +0100 )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: 2019-12-07 04:59:59 +0100

Seen: 1,555 times

Last updated: Dec 07 '19