During the Nazi era in Germany, thousands of European Jews and Communists sought protection in Mexico, many of them artists and writers. And a number of American socialists migrated south during the Red Scare campaign of the 1950s, driven away by Senator Joseph R. McCarthy.

Lorenzo Meyer, a historian and emeritus professor at the Colegio de México in Mexico City, said that Mexico’s welcome of generations of left-wing exiles has been a way for its government to assert independence from the United States without presenting an overt foreign policy challenge.

“Of course, with the United States next door, Mexico cannot be completely sovereign, but that does not stop it from taking some symbolic measures,” Mr. Meyer said.