The constitutional requirement to be the PM is that your government can retain the confidence of the house. There is no explicit requirement for the PM to be an MP.
Mackenzie King lost his seat in the 1945 general election, but remained PM. Eventually one of his backbenchers was encouraged to resign and Mackenzie King won the resulting byelection, getting him back into the house.
Cabinet ministers are also not required to sit in the Commons, but there is a convention that they should. Our late WW2 Minister of Defence (McNaughton) never sat as an MP - he lost two consecutive elections.