What I am looking for

I am looking for a math evaluator in Python which supports variables and evaluation of expressions based on variable assignments.

What I have found

I found python-expression-eval (also available on PyPi) which seemed exactly what I was looking for. In fact, it makes you do the following things.

>>> parser = Parser()
>>> ev = parser.parse('x * x + 2*y + 5')
>>> ev.evaluate({'x': 3, 'y': 4})

Where that call of the evaluate() method means "evaluate the expression x * x + 2*y + 5 in the case where x is 3 and y is 4.

But it is not maintained

Everything is perfect, except that as you can see from the github page the project seems not maintained much. There are only two "recent" changes, one five months ago and one five months ago, but they are both very small and one of the two is just on the README file. The issues opened by the users are not closed since November 2017 and there are no replies by the authors, not even for some issues opened in 2016.

The bugs reported by the users are not fixed, and I have found another important bug which prevents me from debugging my own code. In particular I get a cannot concatenate str and float error when I call the toString() method if there are negative numbers in the math expression (e.g. '-2 + x'). This prevents me from using toString() to check if an expression has been parsed correctly.

Then I ask:

do you know any well-maintained library which is alternative to python-expression-eval?

  • 1
    You may want to add a more challenging example. For yours you do not need a library at all: eval('x * x + 2*y + 5', {"x":3,"y":4} does the job.
    – Wrzlprmft
    Jun 15, 2018 at 14:48
  • I will edit the question soon with a more challenging example I already have in mind (and I will delete this comment of mine afterwards). Jun 15, 2018 at 21:41

1 Answer 1


SymPy is able to do what you are asking for (and much more):

from sympy import sympify
from sympy.abc import x,y

expr = sympify('x * x + 2*y + 5')
expr.subs({ x:3, y:4 })

Note that this is only one of several ways to do what you ask for in SymPy.

It is not only under active development but the backbone of other Python modules. Hence there is a great interest in keeping it maintained, backwards-compatible, and robust.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.