The guidelines specified in PEP8 are just that - guidelines. They're a set of rules to follow when they make sense.
E402 refers to imports only being at the top of a file. This is to stop the following:
import pygame
# 800 lines of pygame stuff
...
import math
# 10 lines of math stuff
...
# Another 800 pygame lines
In the above example, it's very difficult to know that math
is imported. If you need to use math
again at the end of the file, without E402 telling you off, you'll probably import math
again, which is harmless, but sloppy.
In your case, you're not being sloppy. You're specifically setting some things before importing another library, or providing better error messages to users. Simply tell your linter to ignore the warnings on those lines as suggested in the comments, with # noqa: E402
at the end of the line. You can think of this as you telling the linter "I know what I'm doing, go away."
# noqa: E402
? – Terminable