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??
The Python "NameError: name 'sys' is not defined" occurs when we use the sys module without importing it first. To solve the error, import the sys module before using it - import sys .
The sys module in Python provides various functions and variables that are used to manipulate different parts of the Python runtime environment. It allows operating on the interpreter as it provides access to the variables and functions that interact strongly with the interpreter.
Like all the other modules, the sys module has to be imported with the import statement, i.e. The sys module provides information about constants, functions and methods of the Python interpreter.
It lets us access system-specific parameters and functions. import sys. First, we have to import the sys module in our program before running any functions. sys.modules. This function provides the name of the existing python modules which have been imported.
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).
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 sys except 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 sys try: 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 leastsq except 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 imports
try:
import numpy as np
# other unsafe imports
except ImportError:
print "Error: missing one of the libraries (numpy, pyfits, scipy, matplotlib)"
sys.exit()
In addition to the answers given above, check the last line of the error message in your console. In my case, the 'site-packages' path in sys.path.append('.....') was wrong.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With