Microsoft Authentication Library (MSAL) for Python makes it easy to authenticate to Microsoft Entra ID. General docs are available here https://learn.microsoft.com/entra/msal/python/ Stable APIs are documented here https://msal-python.readthedocs.io. Questions can be asked on www.stackoverflow.com with tag "msal" + "python".
Consider backport this Azure CLI improvement into MSAL EX
https://github.com/Azure/azure-cli/pull/20722/files#r769158444
And this one: https://github.com/Azure/azure-cli/pull/20722#discussion_r769207146