BARCELONA, Spain — When he was prime minister of France, Manuel Valls was seen as a possible heir to the presidency. His ambitions ran aground when he lost the primary election of the Socialist Party, which imploded under pressure from the insurgent candidacy of Emmanuel Macron, who now inhabits the Élysée Palace.

Now, Mr. Valls, 56, is looking for a political comeback, this time as a mayoral candidate in Barcelona, which votes on Sunday along with the rest of Spain in local elections that could influence the negotiations over a national government.

Mr. Valls’s circuitous route to candidacy in Barcelona, where he was born, has opened him to charges from opponents of being an opportunistic carpetbagger, a political shape-shifter looking for a second life.

Unusually for a politician, Mr. Valls has not entirely run from the characterization. While he has emphasized his connection to Barcelona, the capital of the region of Catalonia, he has embraced his outsider status.