In python, we would use the scikit-learn library for such kind of computation: https://scikit-learn.org/stable/modules/generated/sklearn.cluster.k_means.html
Or the scipy library: https://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.vq.kmeans.html#scipy.cluster.vq.kmeans
However, they only use the euclidean distance metric. The code for scipy seems easier to edit https://github.com/scipy/scipy/blob/e368cd42cee81df86c55d23d78380b83ca320306/scipy/cluster/vq.py#L279 so you could consider updating it and making a pull request to provide that feature to the library.
Otherwise, someone already asked this question on stack overflow a while back: Create clusters using correlation matrix in Python
It is not about the k-means clustering but it is about clustering based on correlation. Maybe you will find it helpful.