I have a module that is specifically Python 3. What’s the best way to ensure that if someone tries importing it in Python 2 that it blows up/raises some sort of exception?
Right now I have:
# all my imports that I need, ex:
import sys
# blow up if not python 3
if sys.version_info.major < 3:
sys.exit("This is not Python 3")
But I don’t really like the extra import (if my module doesn’t need sys it has to import it for the version check), and this just doesn’t quite “feel right”. Is there a better/more idomatic way?
Don’t sys.exit, it makes other developers want to stab you in the face.
Simply throw either an
ImportErroror somePy3kCompatibleerror of your making.