Fixed #22577: Python 3 broke on non-module migrations directory

We could rely on the fact that this line is the culprit that raises an AttributeError. But that would lead to duplicate code in the except-clauses.