Catalonia's parliament declared independence from Spain on Friday in defiance of the Madrid government, which at the same time was preparing to impose direct rule over the region.

Although the declaration was in effect a symbolic gesture as it will not be accepted by Spain or the international community, the moves by both sides take Spain's worst political crisis in four decades to a new level.

Spanish Prime Minister Mariano Rajoy immediately called for calm and said the rule of law would be restored.

The motion passed in the regional parliament in Barcelona -- which was boycotted by opposition parties -- said Catalonia constituted an independent, sovereign and social democratic state. It called on other countries and institutions to recognise it.

It also said it wanted to open talks with Madrid to collaborate on setting up the new republic.

"It is not going to be easy, it is not going to be free, it is not going to change in a day. But there is no alternative to a process towards the Catalan Republic," lawmaker Marta Rovira of the Junts pel Si pro-independence alliance said in a debate leading to the vote.

After the debate, lawmakers from members of three main national parties -- the People's Party, the Socialists and Ciudadanos, walked out.

Members of the pro-independence parties and the far-left Podemos then voted in 70-10 in favour in a secret ballot aimed at hindering any attempt by the central government to lay criminal charges on them.

Spanish shares and bonds were sold off when the result of the vote was announced.

Catalan leader Carles Puigdemont left the chamber to shouts of "President!".

Meanwhile in Madrid the upper house of Spain's parliament, the Senate, was due to approve Article 155, the law that allowing the central government to take over the autonomous region.

"Exceptional measures should only be adopted when no other remedy is possible," Spanish Prime Minister Mariano Rajoy said in an address to the Senate. "In my opinion there is no alternative. The only thing that can be done and should be done is to accept and comply with the law."

The Catalan leadership was ignoring the law and making a mockery of democracy, he said.

"We are facing a challenge unprecedented in our recent history," said Rajoy, who has staked out an uncompromising position against Catalonia's campaign to break away from Spain.

After the Senate vote, Rajoy was expected to convene his cabinet to adopt the first measures to govern Catalonia directly. This could include sacking the Barcelona government and assuming direct supervision of Catalan police forces.

But how direct rule would work on the ground - including the reaction of civil servants and the police - is uncertain.

Some independence supporters have promised to mount a campaign of civil disobedience, which could lead to direct confrontation with security forces.

The crisis developed after an independence referendum on Oct. 1 was declared illegal by Madrid. Although it endorsed independence, it drew only a 43 percent turnout as Catalans who oppose independence largely boycotted it.

WORRIED, NERVOUS

In Barcelona, crowds of independence supporters were swelling on downtown streets, shouting "Liberty" in the Catalan language and singing traditional Catalan songs.

"I'm worried, I'm nervous like everybody. But freedom is never free," said Jaume Moline, 50, musician.

Montserrat Rectoret, a 61-year-old historian, said: "I am emotional because Catalonia has struggled for 40 years to be independent and finally I can see it."

The crisis has split Catalonia and caused deep resentment around Spain - national flags now hang from many balconies in the capital in an expression of unity.

It has also prompted a flight of business from the wealthy northeastern region and alarmed European leaders who fear the crisis could fan separatist sentiment around the continent.

Catalonia is one of Spain's most prosperous regions and already has a high degree of autonomy. But it has a litany of historic grievances, exacerbated during the 1939-1975 Franco dictatorship, when its culture and politics were suppressed.