Python findet sympy.parsing.mathml sublibary nicht?
Mein Code:
from sympy import symbols, solve
from sympy.parsing.mathml import parse_mathml
x, y, z = symbols('x, y, z')
mathml_str = '<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>x</mi><mo>+</mo><mi>y</mi><mo>=</mo><mn>2</mn></math>'
eqn = parse_mathml(mathml_str)
solution = solve(eqn, x)
print(solution)
Ich benutze Python 3.10.8 und habe sympy ganz normal über "pip install sympy installiert". Warum wird also die sublibary mathml nicht gefunden?
Kannst du prüfen, ob mathml im aktuellen sympy package enthalten ist? Müsste so gehen:
import sympy
dir(sympy.parsing)
Ausgabe ist: ['__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'parse_expr', 'sympy_parser']
1 Antwort
Probier mal bitte sympy neu zu installieren mit:
pip install sympy --upgrade
und prüfe dann nochmal mit
import sympy
dir(sympy.parsing)
ob mathml enthalten ist. Wenn nicht, wurde es wahrscheinlich aus dem sympy package entfernt.
Eine Alternative dazu wäre die lxml library um den mathml string zu parsen.
Hier mal ein Beispielcode:
from lxml import etree
mathml_str = '<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>x</mi><mo>+</mo><mi>y</mi><mo>=</mo><mn>2</mn></math>'
root = etree.fromstring(mathml_str)
for element in root:
print(element.tag, element.text)
Ist nicht mehr aufgetaucht. An die Entfernung hab ich ehrlich gar nicht gedacht. Danke dir!