It was clear that there is some embarrassing reason for this: I used parenthesis for the password in the config file. Python added them to the password, so the password was wrong. When testing on the shell, the shell removed the parenthesis, so the password was correct.