Move import sys
outside of the try
-except
block:
import sys
try:
# ...
except ImportError:
# ...
If any of the imports before the import sys
line fails, the rest of the block is not executed, and sys
is never imported. Instead, execution jumps to the exception handling block, where you then try to access a non-existing name.
sys
is a built-in module anyway, it is always present as it holds the data structures to track imports; if importing sys
fails, you have bigger problems on your hand (as that would indicate that all module importing is broken).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…