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.Wed, 09 Sep 2020 23:29:59 +0200sage terms not defined on importhttps://ask.sagemath.org/question/53374/sage-terms-not-defined-on-import/ 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
Wed, 09 Sep 2020 03:02:05 +0200https://ask.sagemath.org/question/53374/sage-terms-not-defined-on-import/Answer by slelievre for <p>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?</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/53374/sage-terms-not-defined-on-import/?answer=53375#post-id-53375If 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:
- [https://ask.sagemath.org/questions/query:import_statements/](https://ask.sagemath.org/questions/query:import_statements/)
and visit the various results of that query.Wed, 09 Sep 2020 04:40:07 +0200https://ask.sagemath.org/question/53374/sage-terms-not-defined-on-import/?answer=53375#post-id-53375Comment by cybervigilante for <p>If you put things in a .py file suddenly you need to import everything properly.</p>
<p>To know what import statements to write, <code>import_statements</code> is your friend.</p>
<p>Use the <code>import_statement</code> function in a Sage session to figure out the imports.</p>
<p>For instance:</p>
<pre><code>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
</code></pre>
<p>and so on.</p>
<p>Place the corresponding imports near the start of your .py file.</p>
<p>So it will now start:</p>
<pre><code>from sage.calculus.var import var
from sage.plot.point import point
</code></pre>
<p>etc.</p>
<p>For more in-depth discussions, see previously asked questions here
or elsewhere. For instance, search <code>import_statements</code> on Ask Sage:</p>
<ul>
<li><a href="https://ask.sagemath.org/questions/query:import_statements/">https://ask.sagemath.org/questions/query:import_statements/</a></li>
</ul>
<p>and visit the various results of that query.</p>
https://ask.sagemath.org/question/53374/sage-terms-not-defined-on-import/?comment=53385#post-id-53385Thanks. 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 ðŸ˜µWed, 09 Sep 2020 21:10:52 +0200https://ask.sagemath.org/question/53374/sage-terms-not-defined-on-import/?comment=53385#post-id-53385Comment by slelievre for <p>If you put things in a .py file suddenly you need to import everything properly.</p>
<p>To know what import statements to write, <code>import_statements</code> is your friend.</p>
<p>Use the <code>import_statement</code> function in a Sage session to figure out the imports.</p>
<p>For instance:</p>
<pre><code>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
</code></pre>
<p>and so on.</p>
<p>Place the corresponding imports near the start of your .py file.</p>
<p>So it will now start:</p>
<pre><code>from sage.calculus.var import var
from sage.plot.point import point
</code></pre>
<p>etc.</p>
<p>For more in-depth discussions, see previously asked questions here
or elsewhere. For instance, search <code>import_statements</code> on Ask Sage:</p>
<ul>
<li><a href="https://ask.sagemath.org/questions/query:import_statements/">https://ask.sagemath.org/questions/query:import_statements/</a></li>
</ul>
<p>and visit the various results of that query.</p>
https://ask.sagemath.org/question/53374/sage-terms-not-defined-on-import/?comment=53387#post-id-53387Happy to help. If you share a minimal version of the module,
I'm sure people would help improve it.Wed, 09 Sep 2020 23:29:59 +0200https://ask.sagemath.org/question/53374/sage-terms-not-defined-on-import/?comment=53387#post-id-53387