the following code enables to select and highlight the feature with the id 5, further, the highlight is shown in red:
from qgis.core import *
iface.mapCanvas().setSelectionColor( QColor("red") )
l=iface.activeLayer()
l.selectByIds([5])
If all worked as expected, you will see the feature with id 5 highlighted in red in your map view, like so:

However, as one can see, the highlighting is much thinner than the QGIS default highlight.
How can I adjust the width of the hightlight with Pyqgis?
I think at some code line as
iface.mapCanvas().setSelectionWidth( QWidth(0.4) )

layersRemovedsignal of theQgsProject.instance(). This means that the highlights will be cleared if the highlighted layer is removed from the project. – Matt Mar 31 '23 at 09:45