I have a piece of code which is working in Linux, and I am now trying to run it in windows, I import sys but when I use sys.exit(). I get an error, sys is not defined. Here is the begining part of my code
try:
import numpy as np
import pyfits as pf
import scipy.ndimage as nd
import pylab as pl
import os
import heapq
import sys
from scipy.optimize import leastsq
except ImportError:
print "Error: missing one of the libraries (numpy, pyfits, scipy, matplotlib)"
sys.exit()
Why is sys not working??
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).