Some of the death row inmates have formed a choir to sing for freedom
Uganda's Supreme Court has ruled in a case involving more than 400 death row inmates that the death penalty is constitutional.
However, it said that hanging was cruel and recommended that parliament consider another means of execution.
The judges also said it was unreasonable to keep convicts on death row for more than three years.
It means most of the prisoners involved in the case will have their sentences commuted to life in prison.
Although the death penalty has not been used since 1999, the court said it acted as a deterrent to murder.
But the judges also ruled that a mandatory death sentence - a legal term meaning that the penalty for murder is automatically death - was unconstitutional.
Parliament was asked to consider a means of execution other than hanging.
"I would agree with the respondents that hanging as a method of execution as it is carried out in Uganda is a cruel, inhuman and degrading punishment," Justice Egonda-Ntende said.
The BBC's Joshua Mmali in the capital, Kampala, says four prisoners were in court for the ruling.
Susan Kigula, the woman who led the other prisoners in filing the original petition, wept painfully as the actual implications of the ruling dawned on her.
Our reporter says whereas fellow petitioners on death row may escape the death penalty following the ruling, her plight is still unclear.
Her death sentence was only confirmed by an appeal court about a year ago.
It means she could still be hanged within the next two years, unless the president pardons her under the presidential prerogative of mercy.
The prisoners' appeal was supported by Ugandan human rights groups.
The Supreme Court's decisions reaffirmed the verdict of the Constitutional Court in 2005.
The attorney general had sought to overturn the 2005 ruling on the mandatory death penalty.