Tesla Chief Executive Elon Musk has been making headlines for all the wrong reasons in the past few months, and one activist investor thinks it’s because he’s trying to sabotage his own job.

Greenlight Capital’s David Einhorn, who has been shorting Tesla for some time, said in a poison-pen letter Friday to investors that the eccentric billionaire is looking for a way out of his company to avoid the blame if it collapses.

Playing the role of Dr. Freud, Einhorn — who admitted that his fund was down 25.7 percent for the year — wrote that Musk is unable to build the Model 3 without losing money, but that he is too proud to admit defeat and cancel orders.

“His conduct suggests that he is doing his best to be relieved of his position as CEO to avoid accountability,” Einhorn wrote.

Indeed, Musk has been doing and saying all the wrong things as of late. In July, Musk landed in a legal scrap with a hero rescue diver in Thailand, whom he accused of being a “pedo guy” and “child rapist.”

He followed that up with a tweet that landed him in hot water with the Securities and Exchange Commission, writing that he was thinking of taking Tesla private at $420 a share — a pot joke meant to impress his girlfriend, according to the regulator — and that he had “funding secured.”

The fallout of the tweet led to Musk settling fraud charges with the agency last weekend, stepping down as chairman of the Tesla board and he and the company each paying a $20 million fine.

On Thursday, he attacked the SEC again, calling it the “Shortseller Enrichment Commission.”

Tesla’s shares dropped more than 7 percent on Friday, following Musk’s swipe at the regulator.

Jeffrey Sonnenfeld, a professor at the Yale University School of Management, agreed with Einhorn’s theory, noting that Musk seems to be “intoxicated with his own brilliance.”

“He resents anyone second-guessing him and challenging the cult-like reverence he enjoyed until last year,” Sonnenfeld said.

He added that Musk’s behavior makes it seem as if he is “looking for scapegoats” in the event of Tesla’s “self-immolating.”

“He’ll blame the board for taking him out, or the SEC for derailing his genius,” Sonnenfeld said. “He wants to create this sense of martyrdom that fits with this whole cult-like imagery.”

Einhorn’s assessment of the Tesla CEO’s motives should be taken with a grain of salt, however — he and Musk have battled each other through Musk’s tweets and Greenlight’s investment letters.

Last quarter, Einhorn called Musk “erratic and desperate” and said he was “happy” his Model S lease ended, calling out “problems with the touchscreen and power windows.”

Musk responded in a tweet, calling it “tragic.”

“Will send Einhorn a box of short shorts to comfort him through this difficult time,” he wrote.

Tesla shares closed at $261.95.