python:系统未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17761697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:08:01  来源:igfitidea点击:

python: sys is not defined

pythonsys

提问by astrochris

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

我有一段在 Linux 中工作的代码,我现在试图在 Windows 中运行它,我导入了 sys 但当我使用 sys.exit() 时。我收到一个错误,sys 未定义。这是我的代码的开始部分

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

为什么系统不工作?

采纳答案by Martijn Pieters

Move import sysoutsideof the try-exceptblock:

移动import sys以外的中try-except块:

import sys
try:
    # ...
except ImportError:
    # ...

If any of the imports beforethe import sysline fails, the restof the block is not executed, and sysis never imported. Instead, execution jumps to the exception handling block, where you then try to access a non-existing name.

如果该行之前的任何导入import sys失败,则不会执行该块的其余部分,并且sys永远不会导入。相反,执行跳转到异常处理块,然后您尝试访问一个不存在的名称。

sysis a built-in module anyway, it is alwayspresent as it holds the data structures to track imports; if importing sysfails, you have bigger problems on your hand (as that would indicate that allmodule importing is broken).

sys无论如何,它是一个内置模块,它始终存在,因为它拥有跟踪导入的数据结构;如果导入sys失败,则说明您手头有更大的问题(因为这表明所有模块导入都已损坏)。

回答by TerryA

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 foodoesn't exist. Then only datetimewill be imported.

假设foo不存在。然后只会datetime被导入。

What you can do is import the sys module at the beginning of the file, before the try/except statement:

您可以做的是在文件开头的 try/except 语句之前导入 sys 模块:

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

回答by astrognocci

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

此外,您应该在try块中缩进您的代码。

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