简体   繁体   中英

Is it possible to check for correct python syntax of a given file/string from Java

I am writing a piece of code in java, part of this code deals with handling python code. I was just interested if anyone has come across a way of checking if the python code is syntactically correct during runtime. I don't actually need to run the python code, as i'm writing a program that generates small snippets of it for teaching purposes as part of a project.

Is using a system commands the only way to achieve this?

For Python 2, this can be done with Jython :

new org.python.util.PythonInterpreter().compile("python code here")

and an exception will be thrown if it finds a problem (likely org.python.core.PySyntaxError )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM