It is a general coding best practice not to add any kind of secrets or credentials in the code even if the code repository is private.
It is always recommended to create environment variables having credentials. It is also better to have them encrypted and decrypted in the Python script on runtime if the server where this script is supposed to run is going to be used by others as well. There are many other open-source free tools available to manage the secrets.
With respect to your context, check the following code below in Python running in Linux or Windows:
In Python
DB_PASSWORD = os.environ['MYSQL_DB_PASSWORD']
In Linux Terminal
export MYSQL_DB_PASSWORD=Pass1234
In Windows CMD
set MYSQL_DB_PASSWORD=Pass1234