Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: sys is not defined

Tags:

python

sys

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??

like image 267
astrochris Avatar asked Jul 20 '13 11:07

astrochris


People also ask

Why is Sys not defined Python?

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 .

Is SYS included in Python?

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.

Do you need to import sys in Python?

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.

What is import sys Python 3?

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.


4 Answers

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).

like image 134
Martijn Pieters Avatar answered Oct 06 '22 00:10

Martijn Pieters


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() 
like image 30
TerryA Avatar answered Oct 05 '22 23:10

TerryA


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()
like image 32
astrognocci Avatar answered Oct 06 '22 01:10

astrognocci


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.

like image 24
inspiredMichael Avatar answered Oct 06 '22 00:10

inspiredMichael