RICHMOND, Va. — Bob McDonnell, the former governor of Virginia who was convicted in September on corruption charges, was sentenced to 24 months in prison on Tuesday in one of the most spectacular falls of a statewide official in the history of the commonwealth.

Mr. McDonnell, 60, whose lawyers had argued for community service in lieu of prison time, was sentenced by Judge James R. Spencer of Federal District Court, who after a lengthy discourse on sentencing guidelines and the nature of guilt and remorse concluded that “a price must be paid.”

Judge Spencer said he found the situation heartbreaking and the crimes confounding. But “unlike Pontius Pilate,” he said, “I can’t wash my hands of it all.”

The sentence was more than Mr. McDonnell — whose name was once floated as a possible candidate for the White House — had hoped for. But it was far less than the 10 to 12 years recommended by the federal probation office and the six and a half years later sought by prosecutors. Mr. McDonnell will face two years of supervision upon release.

He and his wife, Maureen, were found guilty of trading favors in return for $177,000 in loans, vacations and gifts from a wealthy family friend who was trying to promote his vitamin supplement business. Ms. McDonnell is scheduled to be sentenced Feb. 20.

Mr. McDonnell’s defense centered on his wife’s relationship with the Virginia businessman, Jonnie R. Williams Sr., whose company, Star Scientific, was under federal investigation for possible security violations when prosecutors granted Mr. Williams immunity in exchange for his cooperation with the McDonnell inquiry.

Throughout the trial, defense lawyers portrayed the McDonnells as incapable of conspiring with Mr. Williams because of a bad marriage, and Ms. McDonnell as someone so taken with Mr. Williams that she allowed him to compromise her husband’s office.

Judge Spencer, referring to Mr. Williams, said, “While Mrs. McDonnell may have allowed the serpent into the mansion, the governor knowingly let him into his personal and business affairs.”

Mr. McDonnell, who cried out when he was convicted in September, appeared impassive as the sentence was read. Afterward, he hugged some of his weeping children and kissed the cheek of Ms. McDonnell, who made an unexpected appearance. He was directed to turn himself in on Feb. 9.

Video

Bob McDonnell, former governor of Virginia, thanked the judge for showing mercy with his sentence of two years in prison and apologized for his mistakes in life, saying, “I am a fallen human being.”Published OnJan. 6, 2015CreditImage by Alex Wong/Getty Images

Outside, Mr. McDonnell thanked the judge “for the mercy he displayed to me today,” and said he would immediately appeal.

For hour upon hour before sentence was passed, the court heard accounts from 11 witnesses who recalled Mr. McDonnell’s good deeds and philanthropy: starting a food bank; helping felons get their voting rights restored; visiting the ill, bereaved and incarcerated; and conducting various other acts.

“If he is buried in jail,” said William Horan, the president of Operation Blessing International, a humanitarian organization that offered Mr. McDonnell a place should the judge have opted for community service, “it would be burying someone of enormous value.”

Mr. McDonnell’s sister, Nancy McDonnell Naisawald, called Mr. McDonnell’s ordeal “an unbelievably terrible experience for my family.” She said the fact that her brother, who was governor from 2010-14, could never serve in government again was more than enough punishment.

“He’s lost everything,” she said.

The most animated witness was former Gov. L. Douglas Wilder, the only one prosecutors chose to cross-examine, somewhat to their peril. Mr. Wilder threw oratory barbs their way with clear enjoyment, noting that Mr. Williams, whom he referred to as the “progenitor” of the case, “walks away clean,” producing thundering applause among Mr. McDonnell’s supporters.

Michael S. Dry, an assistant United States attorney who prosecuted the case, noted the moving nature of the comments but said that they did not mitigate the crime.

The Jonnie Williamses “of the world are a dime a dozen,” he told the court. “Corrupt governors are not. But when we find one,” he said, the official must be brought to justice.

Mr. McDonnell, who arrived in court with two of his daughters, largely looked on silently through the hours of testimony from friends and former associates, and in the near hourlong reading of comments from other supportive leaders by his lawyers.

He was the last to address the court. “I stand before you as a heartbroken and humbled man,” he said, as many supporters dabbed at their eyes with tissues. Mr. McDonnell said his life in the governor’s mansion had become unbalanced. Now, he said, he will dedicate the rest of his life “newly to service.”

Mr. Dry, his face twisted in anger, declined to comment as he left the courtroom. He had urged the judge to base his sentence on that of Phillip A. Hamilton, a former member of the Virginia House of Delegates who was convicted in 2011 of bribery and extortion and was sentenced to nine and a half years. Mr. McDonnell held a higher office, Mr. Dry said, and his misdeeds went on longer.

But the judge said that Mr. McDonnell’s case demonstrated the need for judicial discretion. “This whole case has been tragic from beginning to end,” Judge Spencer said.

A version of this article appears in print on , on Page A11 of the New York edition with the headline: Former Virginia Governor Is Sentenced to 24 Months for Corruption. Order Reprints | Today’s Paper | Subscribe