Apply for D7 Visa or Apply To School for Residence Visa?

Hello,

I am an American living overseas for years now and have decided to move to Portugal next spring, 2023. However, I am trying to figure out if it is necessary for me to have to fly back to the States and apply for certain paperwork for either a D7 visa or applying to go to school in Portugal and receiving a residency permit this route.

I would like to attend University in Portugal anyway, but I would prefer not to have to fly back to America to apply.

Has anyone gone through this process and can give me insight as to what is absolutely necessary to apply for either one and if there are alternative methods to flying to the States to apply?