0

I've been trying to evaluate a simple "integrate(x,x)" statement from within Python, by following the Sage instructions for importing Sage into Python. Here's my entire script:

    #!/usr/bin/env sage -python

from sage.all import *
def main():
    integrate(x,x)
    pass
main()

When I try to run it from the command line, I get this error thrown:

NameError: global name 'x' is not defined

I've tried adding var(x) into the script, global x, tried replacing integrate(x,x) with sage.integrate(x,x), but I can't seem to get it to work, I always get an error thrown.

The command I'm using is ./sage -python /Applications/path_to/script.py

I can't seem to understand what I'm doing wrong here.

Edit: I have a feeling it has something to do with the way I've "imported" sage. I have my a folder, let's call it folder 1, and inside of folder 1 is the "sage" folder and the "script.py"

I am thinking this because typing "sage." doesn't bring up any autocomplete options.

  • why is it in another function main? – Padraic Cunningham Jul 06 '14 at 03:10
  • I don't understand what you mean by this. I tried removing the def main(): and main() lines and that didn't change anything. –  Jul 06 '14 at 03:12
  • I just thought it was convention to put code in a main() function. I learned Java before Python and code wouldn't run without a main(). –  Jul 06 '14 at 03:12
  • i imagine integrate is a function in it's own right so you can call it directly, the docs don;t show any usage as you have above, I had no issues running their examples. – Padraic Cunningham Jul 06 '14 at 03:15
  • So could you please explain the changes in my code I'd need to make? –  Jul 06 '14 at 03:15
  • I tried running sage.integrate without the main function and got 'AttributeError: 'module' object has no attribute 'integrate'' –  Jul 06 '14 at 03:17
  • have you checked out the example code? Also what parameters is integrate supposed to take? – Padraic Cunningham Jul 06 '14 at 03:18
  • on the sagecellserver integrate(x,x) works just fine. and yes I have looked at their example code. I don't see anything different from what I'm doing. –  Jul 06 '14 at 03:20
  • Putting everything inside a `main` function is not a common convention in python. – Luca De Feo Jul 06 '14 at 13:09

2 Answers2

2

The name x is not imported by import sage.all. To define a variable x, you need to issue a var statement, like thus

var('x')
integrate(x,x)

or, better,

x = SR.var('x')
integrate(x,x)

the second example does not automagically inject the name x in the global scope, so that you have to explicitly assign it to a variable.

Luca De Feo
  • 704
  • 5
  • 12
  • I will mark your answer as correct but I am wondering, so there is no way to evaluate normal sage code from within python? because within sage, integrate(x,x) would work without declaring x as a variable –  Jul 06 '14 at 15:50
  • Normal sage code is not python code. For example, `(1..2)` is valid sage syntax, while it is a syntax error in python. So no, it makes no sense running "normal Sage code" in python. If you want to execute sage code, why not write it in a `.sage` file? – Luca De Feo Jul 06 '14 at 16:31
  • Good point, Luca - and I think this might be one of the only things that isn't imported into the global namespace that is in a standard Sage session. Can you think of any others? – kcrisman Jul 07 '14 at 12:30
  • Can't think of any other, I must admit I was surprised to learn it is not. – Luca De Feo Jul 07 '14 at 22:01
1

Here's what Sage does (see the file src/sage/all_cmdline.py):

from sage.all import *
from sage.calculus.predefined import x

If you put these lines in your Python file, then integrate(x,x) will work. (In fact, sage.calculus.predefined just defines x using the var function from sage.symbolic.ring; this just calls SR.var, as suggested in the other answer. But if you want to really imitate Sage's initialization process, these two lines are what you need.)

John Palmieri
  • 1,531
  • 8
  • 13