There are numerous python API libraries we can use to access GPIO from py scripts.
GPIO Zero Lib is NOT preinstalled on retropie.
https://gpiozero.readthedocs.io/en/stable/installing.html
So I guess just always install it on fresh retropie image.
sudo apt update press Y
sudo apt install python3-gpiozero