You can avoid the pain if you make some exercises that make your piriformis muscle to relaxation and stretching. In order to decrease your pain and increase your ability for moving, there are several relaxation and stretching exercises that can be applied to muscles of the kneecap and hip (hamstring) and hip muscles.
Firsly, you should lie on your back and keep your legs straight. Place your foot on the sore leg on the outside of your knee on the other leg. Then push your kneeling knee gently until you feel stretching of your muscle towards your other leg. Keep your position cactive for 30 seconds. Afterwards, slowly return to the first position. Do not stretch too much. Moreover, do not try to bring your knee too much closer to the floor. Repeat this exercise three times.
Lie on your back with your feet on the ground. Put your sore leg on your other knee. Put your hands behind your knees in a slightly lower position. Afterwards, lift your legs towards your shoulder on the side of your aching leg gently, until you feel stretching in your muscles. Keep your position for 30 seconds. Then, return to your first position slowly. Do this exercise for 3 times.