One of the commandments of a Jewish king is not to have too many horses, so that the Jewish people won't go back to live in Egypt, where the best horses came from. Question: till when was this valid? We see that there was a community and great Rabbis who did live in Egypt later on. And why not live in Egypt?
The prohibition of living in Egypt has never changed. The question is to who did it apply to in the first place?
There are various opinions among the Rishonim Among them are that it only applies to
(1) When the Egyptians residing in Egypt themselves were the descendants of the Egyptians who lived there in biblical times . That stopped being the situation after Sancherib and the Assyrians conquered it and made mass population moves
(2)It only applies to leaving Israel to settle in Egypt. Not coming there from another country
(3)It only applies when Israel is ruled by Jewish kings ( A bibical kingdom. Not the modern state of Israel. )
There are various other explanations for why Jews have lived in Egypt. The common denominator is that there are limitations to the circumstances when the Torah prohibited living there
How could Jews justify living in Egypt in spite of the commandment not to do so? Over time, some commentators softened the commandment. First, the Talmud allowed temporary returns:
You may not return [to Egypt] to dwell there, but for the purpose of business or war you may return. [Sanhedrin Y 10:7]
Some said the commandment only applies to the entire Jewish people, not to a subset. Others said it applies only to Jews leaving Israel, not any other country, for Egypt. The Radbaz argued that if you are forced to stay in Egypt by the government you must comply. Rabbenu Bahya said it applied only in biblical times. The Semag and Ritva said the prohibition no longer applies because today's Egyptians are not the descendants of those who lived in Pharaoh’s day. Ritva added that the prohibition applies only when Jews rule the Land of Israel. (Note that they didn’t then, but do now!)