Ask Your Question
1

sage terms not defined on import

asked 2020-09-09 03:02:05 +0100

cybervigilante gravatar image

I made a small routine for Sagemath 9.1 to show the most well-defined colors (with names) for my monitor. Works fine. But when I make it into a module then import it into Sagemath, the program chokes on terms which are defined in sagemath, such as "var" and "point". I even tried importing sage from the __init__.py program, but no luck. What's going wrong?

def colorlist():
    colors = ['red', 'green', 'blue', 'black', 'orange', 'purple', 'cyan', 'magenta', 'lime', 'pink', 'teal', 
              'brown', 'maroon', 'olive', 'navy', 'grey']
    var('x y')
    point_list = []
    name_list = []
    text_list= []
    x = 0; y = 0
    for color in colors:
        x+=1;y+=1
        name_list.append(color)
        point_list.append(point((x,y), rgbcolor=color,size=50))
        text_list.append(text(color,(x,y), color="black"))

    new_list = point_list + text_list
    return sum(new_list)  

ERROR
/opt/sagemath-9.1/local/lib/python3.7/site-packages/colors/showcolors.py in showthem()
      9         x+=1;y+=1
     10         name_list.append(color)
---> 11         point_list.append(point((x,y), rgbcolor=color,size=50))
     12         text_list.append(text(color,(x,y), color="black"))
     13 

NameError: name 'point' is not defined
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2020-09-09 04:40:07 +0100

slelievre gravatar image

updated 2020-09-09 14:30:18 +0100

If you put things in a .py file suddenly you need to import everything properly.

To know what import statements to write, import_statements is your friend.

Use the import_statement function in a Sage session to figure out the imports.

For instance:

sage: import_statements(point)
# ** Warning **: several names for that object: point, points
from sage.plot.point import point

sage: import_statements(var)
from sage.calculus.var import var

and so on.

Place the corresponding imports near the start of your .py file.

So it will now start:

from sage.calculus.var import var
from sage.plot.point import point

etc.

For more in-depth discussions, see previously asked questions here or elsewhere. For instance, search import_statements on Ask Sage:

and visit the various results of that query.

edit flag offensive delete link more

Comments

Thanks. After I dug down the imports worked, although only in the main program, not in __init__.py. I'll figure that out later - I just wanted to get unconfused about module making 😵

cybervigilante gravatar imagecybervigilante ( 2020-09-09 21:10:52 +0100 )edit

Happy to help. If you share a minimal version of the module, I'm sure people would help improve it.

slelievre gravatar imageslelievre ( 2020-09-09 23:29:59 +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: 2020-09-09 03:02:05 +0100

Seen: 640 times

Last updated: Sep 09 '20