Uganda’s Constitutional Court on Tuesday largely upheld a sweeping anti-gay law that President Yoweri Museveni signed last year, undermining the efforts of activists and rights groups to abolish legislation that drew worldwide condemnation and strained the East African nation’s relationship with the West.
The legislation, which was signed into law by Mr. Museveni in May, calls for life imprisonment for anyone who engages in gay sex. Anyone who tries to have same-sex relations could face up to a decade in prison.
Uganda has faced international consequences for passing the law, with the World Bank suspending all new funding and the United States imposing sanctions and visa restrictions on top Ugandan officials. But the law was popular in Uganda, a landlocked nation of over 48 million people, where religious and political leaders frequently inveigh against homosexuality.
The fallout for Uganda will be watched closely in other African countries where anti-gay sentiment is on the rise and anti-gay legislation is under consideration, including Kenya, Namibia, Tanzania and South Sudan. In February, Ghana’s Parliament passed an anti-gay law, but the country’s president said that he would not sign it until the Supreme Court ruled on its constitutionality.
In the Ugandan case, Frank Mugisha, a prominent human rights activist and one of the petitioners, said that they would appeal the Constitutional Court’s decision to the Supreme Court.
The law in Uganda decrees the death penalty for anyone convicted of “aggravated homosexuality,” a sweeping term defined as acts of same-sex relations with minors or disabled people, those carried out under threat or while someone is unconscious. Even being accused of what the law refers to as “attempted aggravated homosexuality” carries a prison sentence of up to 14 years.
Passage of the law — which also imposes harsh fines on organizations convicted of promoting homosexuality — alarmed human rights advocates, who said it would give new impetus for the introduction of equivalent draconian laws in other African nations. Uganda is among the African countries that already ban gay sex, but the new law creates additional offenses and prescribes far more punitive penalties.
The United Nations, along with local and international human rights groups, said that the law conflicted with Uganda’s Constitution and that it would most likely be used to harass and intimidate its L.G.B.T.Q. population.
The law was first introduced in early March by a lawmaker who said that homosexuality was becoming pervasive and threatening the sanctity of the Ugandan family. Some legislators also claimed that their constituents had notified them of alleged plans to promote and recruit schoolchildren into homosexuality — accusations that rights groups said were false.
Anti-gay sentiment is prevalent among Muslim and Christian lawmakers and religious leaders from both faiths. They say that homosexuality is a Western import, and they held rallies to show support for the law before it passed.
A few weeks after it was introduced in Parliament, the law was quickly passed with only two lawmakers opposing it.
Activists, academics and human rights lawyers who challenged the law in court said it contravened not only Uganda’s Constitution, which guarantees the right to privacy and freedom from discrimination, but also international treaties, including the African Charter on Human and Peoples’ Rights. They also argued that the law was passed too quickly by Parliament, with not enough time allowed for public participation.
Human rights groups said that since the law was introduced and passed, L.G.B.T.Q. Ugandans have faced intensive violence and harassment.
Convening for Equality, a coalition of human rights groups in Uganda, has documented hundreds of rights violations and abuses, including arrests and forced anal examinations. Gay and transgender Ugandans have also been evicted from their homes and beaten up by family members — forcing many to flee to neighboring countries like Kenya. In early January, Steve Kabuye, a prominent gay rights advocate, was stabbed in an attack that activists said was spurred by homophobia linked to the law. Mr. Kabuye has since fled to Canada with the help of a nongovernmental organization.
The law’s passage brought swift repercussions for Uganda, too. Health experts also worried the law would hinder medical access for gay people, especially those seeking H.I.V. testing, prevention and treatment.
The United States said it would restrict visas for current and former Ugandan officials who were believed to be responsible for enacting the anti-gay policy. The Biden administration also issued a business advisory for Uganda and removed the country from a special program that allows African products duty-free access to the United States.
The World Bank, citing the anti-gay law, also said in August it would halt all future funding to Uganda. The economic pressures continued to pile on, with foreign travelers and investors staying away from Uganda.
Ahead of the ruling, Mr. Museveni remained publicly defiant, but analysts and diplomats said he privately worried about his country’s being labeled an outcast, and the devastating economic repercussions it was causing.