python: sys is not defined
Move import sys
outside of the try
-except
block:
import systry: # ...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).
You're trying to import all of those modules at once. Even if one of them fails, the rest will not import. For example:
try: import datetime import foo import sysexcept ImportError: pass
Let's say foo
doesn't exist. Then only datetime
will be imported.
What you can do is import the sys module at the beginning of the file, before the try/except statement:
import systry: import numpy as np import pyfits as pf import scipy.ndimage as nd import pylab as pl import os import heapq from scipy.optimize import leastsqexcept ImportError: print "Error: missing one of the libraries (numpy, pyfits, scipy, matplotlib)" sys.exit()
I'm guessing your code failed BEFORE import sys, so it can't find it when you handle the exception.
Also, you should indent the your code whithin the try
block.
try:
import sys# .. other safe importstry: import numpy as np # other unsafe importsexcept ImportError: print "Error: missing one of the libraries (numpy, pyfits, scipy, matplotlib)" sys.exit()