A Brooklyn federal judge on Thursday urged the U.S. Bureau of Prisons to hold a convicted child molester in a medical facility and said he would find the 15-year mandatory minimum sentence unconstitutional if the bureau doesn’t comply.

The apparently unprecedented move by U.S. District Judge Jack Weinstein, who said defendant “D.W.” — identified on the court docket as Darnell Washington — had mental problems and would be a suicide risk in the general prison population, reflected the judge’s long-standing criticism of mandatory minimums.

Weinstein said Washington, 27, of Brooklyn, a repeat offender convicted of both child pornography charges and sexual exploitation of a minor, had been abused as a child, raped during an earlier prison stint, identified as gay and was suicidal.

The judge said 15 years in a regular prison would make him “uniquely vulnerable” to abuse or solitary confinement, and amount to cruel and unusual punishment. He said the time should be served at the Federal Medical Center prison in Devens, Massachusetts, where sex-offender treatment is available, or another medical facility.

The Bureau of Prisons is not obligated to follow a judge’s preference, but Weinstein said if his recommendations were ignored and Washington was put in “general population of a medium or high security prison” he was “prepared” to find the sentence unconstitutional.

“The court is required . . . to impose a sentence of fifteen years in prison on this defendant,” Weinstein wrote in his 215-page ruling. “But, it has the responsibility and power to ensure that the sentence is carried out in a civilized way.”

Deirdre von Dornum, Washington’s lawyer, called it an “appropriate sentence” and said “there is nothing unusual about Judge Weinstein taking his medical condition and extraordinary vulnerability into account.”

The Brooklyn U.S. attorney’s office declined to comment.