Index a BAM file using pysam (How) can you index a BAM file using pysam?

When I tried the intuitive pysam.index I got:

import pysam
my_bam = pysam.AlignmentFile("regular_bwamem_mapping.bam", "rb")
pysam.index(my_bam)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-5-a0b3a04ecb2c&gt; in &lt;module&gt;
----&gt; 1 pysam.index(L_bam)

~/miniconda3/lib/python3.7/site-packages/pysam/utils.py in __call__(self, *args, **kwargs)
     59             args,
     60             catch_stdout=kwargs.get("catch_stdout", True),
---&gt; 61             save_stdout=kwargs.get("save_stdout", None))
     62
     63         if kwargs.get("split_lines", False):

pysam/libcutils.pyx in pysam.libcutils._pysam_dispatch()

TypeError: object of type 'pysam.libcalignmentfile.AlignmentFile' has no len()
Oh you silly sausage, pysam.index takes a bam file name, not a python object.
import pysam
pysam.index(&quot;regular_bwamem_mapping.bam&quot;)

will index your .bam file.