Libya's prime minister Abdullah al-Thani has stepped down, saying he and his family had been the victims of a "traitorous" armed attack the previous day.

Mr Thani quit less than a week after parliament tasked him with forming a new cabinet and a month after it ousted his predecessor for failing to rein in the lawlessness gripping the North African country.

He said in a statement that he would not accept the premiership after a "traitorous attack" on himself and his family, but he would stay on in a caretaker capacity until a new prime minister is appointed.

Amid controversy over his appointment, Mr Thani, who was defence minister under ousted premier Ali Zeidan, was named on Tuesday.

"I will not accept Libyans killing each other over this post," Mr Thani said in his statement addressed to the General National Congress (GNC).

Mr Thani said the attack had terrorised inhabitants of a residential district and "put the lives of some of them at risk", without giving specific details.

A source close to Mr Thani said the incident took place on the road from the capital to its airport and caused no casualties.

As premier, Mr Thani was faced with the daunting task of bringing former rebel brigades to heel following the 2011 NATO-backed uprising that ended Muammar Gaddafi’s four-decade rule.