import fileinput
import re
import sys
def setParam(param, value):
value = value.replace('"', '\"') # Экранируем двойные кавычки
for line in fileinput.input("config.py", inplace=True):
line = re.sub('^(\s*%s = ).*$' % param, '\\1"%s"' % value, line)
sys.stdout.write(line)
setParam('VAR1', 'bye')