Troubleshooting standalone scripting issues (seeking knowledge about interpreter and preparser)

29 views
Skip to first unread message

nwi...@oberlin.edu

unread,
Jul 8, 2017, 1:08:55 AM7/8/17
to sage-support
Hello all,

I am currently introducing myself to Sage, already having a fair level of python experience. I have installed SageMath 7.5.1 via the Launchpad PPA onto my Chromebook, which is running an Ubuntu 14.04 chroot on Intel x86_64 architecture.

I wish to use Sage via standalone scripts, as detailed in the official tutorial. However, I am having trouble running their example factoring script. When I attempt to factor polynomials, such as by giving the script the argument "32*x^5-1", I get the following error trace:

Traceback (most recent call last):
  File "./test", line 11, in <module>
    print(factor(sage_eval(sys.argv[1])))
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/misc/sage_eval.py", line 201, in sage_eval
    return eval(source, sage.all.__dict__, locals)
  File "<string>", line 1, in <module>
NameError: name 'x' is not defined

I found that to make the script work, I need to add var('x') to declare x, as well as a dictionary, {'x':x}, for the locals parameter of the sage_eval function. I'm confused, because everything I've read says that x is already declared before the interpreter starts running my script, hence why the code in the tutorial doesn't need to declare it or notify sage_eval. I'd like to make sure that my installation can run code as given, and that it is generally compliant with information from official sources, and I'd also prefer (but am not terribly attached to) keeping with the PPA. Sorry for being lazy, but I haven't tried building the latest version from source and trying that. I'll post up if that solves the issue.

P.S. I know the guidelines asked to keep a single post confined to one question, but my ultimate goal here is to understand the functionality of the interpreter, preparser, and execution of the sage binary. I'd like to have fully fledged standalone scripts, that is, a Sage script (that doesn't need to end in .sage) that can be preparsed and executed with a single call to some utility. It would be great if I could manage to preparse into memory and let the interpreter run the script from there, but my general goal is just redirection of preparser output so that my work environment isn't cluttered with .py scripts that I need to manually organize. I just need to grasp the interpreter and preparser enough so that I know what combination of Sage development and shell scripting can be used to achieve this. Any pointers would be greatly appreciated.

Thanks,
-Nick

Dima Pasechnik

unread,
Jul 8, 2017, 3:47:49 AM7/8/17
to sage-support
Do you see the same issue if you start Sage directly, rather than via this script? (which looks like an old artefact to me, which we should fix)?

nwi...@oberlin.edu

unread,
Jul 8, 2017, 12:19:39 PM7/8/17
to sage-support
From the Sage interactive shell, I can load or attach a script "test.sage" that just has the two lines print(factor(2006)) and print(factor(32*x^5)), and it runs exactly as expected with no errors! Furthermore, I can't find a test.sage.py on my system, which I like but confuses me. I suppose I should have tried this earlier, so thanks very much for your suggestion. This is actually better than what I had in mind, because now I just need to load Sage once and not every time I run a script.

I'm going to hold off on marking this as complete for a little bit, in case anyone has a idea what my Sage is behaving so differently from what the tutorials expect.

nwi...@oberlin.edu

unread,
Jul 8, 2017, 2:27:09 PM7/8/17
to sage-support
It seems as though the preparser only writes the python script to disk if I run "sage test.sage", which might have been what you were actually suggesting.

Jeroen Demeyer

unread,
Jul 10, 2017, 6:00:43 AM7/10/17
to sage-s...@googlegroups.com
Confirmed, this is a bug in the tutorial.
Reply all
Reply to author
Forward
0 new messages