In python when you use the bedtools sorting function, chromosomes are sorted lexicographically, so chr12 will come before chr9.
There are some ways to solve it when you are using bash such as - k1,1 - k2,2n
which many sites described but none of them works in python syntax.
my code is as following:
import pybedtools as bt
sites = bt.BedTool(input.bed).sort().merge(d=-1, c=5, o='sum').saveas('output.bed')