The only thing that really works for me is when I make it a 25 minute hyper-focussed challenge: Set a timer and make the maximum progress that is theoretically possible in that time. No getting water, no toilet breaks, no looking at the phone. Beats 3 hours of getting a glass of water, toilet breaks, getting hungry, realising I should work out and shower first and finding more reasons to jump up any day - surprisingly. Got to always treat it as if it were a competition.
If I ever go back to studying, it definitely has to be from home. Might even have worked out the first time then. Over 50 % of my energy went into the logistics of being at a specific location at a specific time with coursework done, and picking up the course certificates. Yes, I did all the courses for an intermediate diploma and more (back before BSc and MSc was a thing), but failed in picking up 20 % of them before they were destroyed.