I want to be able to check if a function is compatible with Python 3 from the python code (not through a terminal). A string containing the function would be passed into the check.

As far as I know I can only use 2to3 in the terminal to convert Python documents from Python 2 to Python 3. Is there a way I can use this or something else to first check the code in a string and, if it needs to be converted to Python 3, to write the necessary changes back to the string?

How I would like it to work:

def checkCodeCompatibility(functionInString):
    if codeIncompatibleWithPython3:
         return convertCodeToPython3(functionInString)
0

There are 0 best solutions below