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.Sat, 09 Jan 2016 18:25:28 +0100Running Sage inside Pythonhttps://ask.sagemath.org/question/32126/running-sage-inside-python/ I was following a [tutorial](http://doc.sagemath.org/html/en/tutorial/programming.html#standalone-python-sage-scripts). I tried to run this code, but got sage is not recognized error. I am using Ubuntu (Mint). I added the sage root to my path. Now when I print $PATH in the terminal there is the sage path indeed. However, my script still does not recognize the Sage package. What may I be doing wrong? If any additional info is needed I will add it.
Here is the output of running $PATH:
> bash:
> /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/goran/Sage/sage-6.9-x86_64-Linux/sage
I am wondering what my header should look like (of the Python script). In the given example it is
#!/usr/bin/env sage.
I have been trying to adapt some other answers but none worked for me. What I want to achieve is calling Sage in Python code, because I have already written a couple of Python modules, and no I just need to call some Sage functions.
Thank you for the understanding! gilievSat, 09 Jan 2016 18:25:28 +0100https://ask.sagemath.org/question/32126/Can't run Python script in Sage, error at import sage.allhttps://ask.sagemath.org/question/23458/cant-run-python-script-in-sage-error-at-import-sageall/ I only just figured out how to run Sage in a daemon/service-like kind of state - see http://ask.sagemath.org/question/23431/running-sage-from-other-languages-with-higher-performance for the Sage script.
Now I'm trying to rewrite it to Python with Sage imports, so I can automatically start it with `sage -python [PATH TO PYTHON FILE]` as opposed to manually starting Sage and loading the script.
The fails with the following stack trace
Traceback (most recent call last):
File "socket.py", line 1, in <module>
import socket
File "/var/www/lingapps/assets/sage/socket.py", line 4, in <module>
import sage.all
File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/all.py", line 54, in <module>
from sage.env import SAGE_ROOT, SAGE_DOC, SAGE_LOCAL, DOT_SAGE, SAGE_ENV
File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/env.py", line 84, in <module>
_add_variable_or_fallback('HOSTNAME', socket.gethostname())
AttributeError: 'module' object has no attribute 'gethostname'
This also happens when I have just a single line in the script: `import sage.all`.
It also fails with the following error if I just start `sage -python` and type `import sage.all`:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/all.py", line 54, in <module>
from sage.env import SAGE_ROOT, SAGE_DOC, SAGE_LOCAL, DOT_SAGE, SAGE_ENV
File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/env.py", line 20, in <module>
import os, socket
File "socket.py", line 6, in <module>
import sage.calculus.predefined
File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/calculus/__init__.py", line 1, in <module>
import all
File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/calculus/all.py", line 1, in <module>
from calculus import maxima as maxima_calculus
File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/calculus/calculus.py", line 400, in <module>
from sage.rings.all import RR, Integer, CC, QQ, RealDoubleElement, algdep
File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/rings/all.py", line 21, in <module>
from ring import Ring
File "ring.pyx", line 1, in init sage.rings.ring (sage/rings/ring.c:18156)
File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/structure/__init__.py", line 1, in <module>
import dynamic_class # allows for sage.structure.dynamic_class?
File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/structure/dynamic_class.py", line 119, in <module>
from sage.misc.cachefunc import weak_cached_function
File "cachefunc.pyx", line 421, in init sage.misc.cachefunc (sage/misc/cachefunc.c:17825)
File "function_mangling.pyx", line 39, in init sage.misc.function_mangling (sage/misc/function_mangling.c:3106)
File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/misc/sageinspect.py", line 131, in <module>
from sage.env import SAGE_SRC
ImportError: cannot import name SAGE_SRC
It doesn't work with `sage [PATH TO SAGE FILE]`either:
Traceback (most recent call last):
File "socket.py", line 2, in <module>
from sage.all_cmdline import * # import sage library
File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/all_cmdline.py", line 18, in <module>
from sage.all import *
File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/all.py", line 54, in <module>
from sage.env import SAGE_ROOT, SAGE_DOC, SAGE_LOCAL, DOT_SAGE, SAGE_ENV
File "/var/www/lingapps/assets/sage/local/lib/python2.7/site-packages/sage/env.py", line 20, in <module>
import os, socket
File "/var/www/lingapps/assets/sage/socket.py", line 3, in <module>
_sage_const_102400 = Integer(102400); _sage_const_8888 = Integer(8888); _sage_const_0 = Integer(0); _sage_const_10 = Integer(10); _sage_const_1 = Integer(1)
NameError: name 'Integer' is not defined
I'm following the method explained at http://www.sagenb.org/doc/static/faq/faq-usage.html (How do I import Sage into a Python script?).
I assume it has something to do with the enviroment variables, but I can't seem to find any documentation about that on the Sage website so what to do?
I'm running Sage 5.13 on Debian - not installed via `apt-get`, just downloaded and unpacked.WoodgnomeThu, 17 Jul 2014 16:53:53 +0200https://ask.sagemath.org/question/23458/2D stem plothttps://ask.sagemath.org/question/10164/2d-stem-plot/Hi, this is not really a question but I come by these days to search for a stem plot, can't find any useful and finally came up with the following solution, I want to share:
def stem(data,offset=0,**kwargs):
p=point([])
l=line([])
for d in range(len(data)):
p+=point((d+offset,data[d]),size=50)
l+=line([(d+offset,0),(d+offset,data[d])])
show(p+l,xmax=len(data),**kwargs)
for example:
values=[]
for v in range(0,5):
values.append(1/4^v)
stem(values,ymax=1,axes_labels=('n','x[n]'))
#or with offset
stem(values,-4,ymax=1,axes_labels=('n','x[n]'))
- arg1 list of data
- arg2 gives you the opportunity to adjust the starting point
- **kwargs passes all following arguments to the show-functionfarnzworldWed, 29 May 2013 14:57:50 +0200https://ask.sagemath.org/question/10164/getting user imput in Python scriptshttps://ask.sagemath.org/question/9850/getting-user-imput-in-python-scripts/How can I write a script that will get user input when the script runs. I am talking about the sage notebook() here rather than the cli sage.nerak99Tue, 26 Feb 2013 04:09:24 +0100https://ask.sagemath.org/question/9850/How to convert .sage to .pyhttps://ask.sagemath.org/question/9822/how-to-convert-sage-to-py/Hi!
I created the file "example.sage" in my home directory and ran
sage: load "example.sage"
But Sage doesn't convert it to .py file, so I can't get the "example.sage.py" file. What should I do to get it? Actually, my goal is to write a GUI application, based on few sage classes. Can I do it without converting my sage code to the Python code?
yefremWed, 20 Feb 2013 11:51:09 +0100https://ask.sagemath.org/question/9822/