Ask Your Question

problems with sage_eval and eval

asked 2017-01-18 14:30:26 +0200

stan gravatar image

Dear all, I am trying to construct and evaluate sage code, but what used to work in previous versions does not seem to work in SageMath 7.3 any more. Apparently eval('opr(16*a^2*x^2)') does not pre-parse the exponent properly:

var('a x')
term1 = 16*a^2*x^2
print (opr(term1))
print eval('opr(term1)')
print eval('opr(16*a^2*x^2)')


RuntimeError: Use ** for exponentiation, not '^', which means xor
in Python, and has the wrong precedence.

In contrast, if I use sage_eval, I get a different error:

term1 = 16*a^2*x^2
print (opr(term1))
print sage_eval('opr(term1)')
print sage_eval('opr(16*a^2*x^2)')


NameError: name 'opr' is not defined

What would be the correct way of evaluating a text string in a similar way as if it had been entered directly into a code cell?

edit retag flag offensive close merge delete


The errors are both correct. Do you want a Python cell or a Sage cell? What is opr? Maybe you need to import a Python library first in the Sage case.

kcrisman gravatar imagekcrisman ( 2017-01-18 15:44:42 +0200 )edit

Are you working in the Sage REPL, in the Sage Notebook, in the Jupyter notebook? If you are working in the Jupyter notebook, are you using the Python2 kernel, or the SageMath kernel? Are you working on your own machine, or in SageMathCloud? What version of Sage were you using previously where your code was working?

slelievre gravatar imageslelievre ( 2017-01-18 15:53:19 +0200 )edit

Sorry for the missing information. Jupyter with SageMath 7.3 kernel, own machine. opr was an operator, and I actually 'reduced' the problem to an incomplete and meaningless one. Thierry's response below solved it, though.

stan gravatar imagestan ( 2017-01-19 18:03:46 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2017-01-19 12:17:52 +0200

tmonteil gravatar image

updated 2017-01-19 17:51:31 +0200

Regarding the first error, it is fortunate that Sage does not preparse what is inside a string (if it was the case before 7.3, then it was a huge bug). If you want your code withing a string to be preparsed, just use the preparse Sage function, for example:

sage: var('a x')
(a, x)
sage: print eval(preparse('16*a^2*x^2'))

Regarding your second error, well, opr is indeed not defined. Note that you have to pass some context to sage_eval:

sage: var('a x')
(a, x)
sage: sage_eval('16*a^2*x^2') 
NameError: name 'a' is not defined
sage: sage_eval('16*a^2*x^2', locals={'x':x,'a':a})
edit flag offensive delete link more


Oops, sorry, I forgot a few lines when trying to reduce the problem to a minimal example... The preparse() tip solved my problem. I also didn't realise that sage_eval does not use the global namespace. Thanks!!

stan gravatar imagestan ( 2017-01-19 18:01:25 +0200 )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


Asked: 2017-01-18 14:30:26 +0200

Seen: 1,071 times

Last updated: Jan 19 '17