I recently had a similar issue using PySerial in conjunction with the Robot Framework. The above solutions did not work but helped me find one that did:
When connecting I had to set rtscts=False and dsrdtr=True,
my_serial = serial.Serial(port, baudrate, timeout = 5, rtscts=False, dsrdtr=True)
then
my_serial.setDTR(True)
my_serial.setRTS(False)
Then on disconnect
my_serial.setDTR(True)
my_serial.close()
This stopped the ESP32 continually resetting after the script finished.