Note that the accept answer suggests using the OpenCensus Python SDK which has been retired as of July 2023.
Instead, Microsoft suggest switching across to their OpenTelemetry offering and provide the following migration guidance as well as a guide for how to get started with azure-monitor-opentelemetry
.