The following is the code. When I run, I got an error message, saying that "name exit is not defined". Could anyone tell me why? Thanks very much for your time and attention.
以下是代码。当我运行时,我收到一条错误消息,说“名称退出未定义”。谁能告诉我为什么?非常感谢您的时间和关注。
if len(sys.argv) == 4:
### do something
pass
else:
print
"usage: #### something here"
exit(-1)
2 个解决方案
#1
You need to import sys
first, since exit
(and argv
) is in that module.
您需要先导入sys,因为exit(和argv)位于该模块中。
The error I get when I run your code is:
我运行代码时遇到的错误是:
File "", line 1, in NameError: name 'sys' is not defined
NameError中的文件“”,第1行:名称'sys'未定义
which is complaining about sys.argv
instead of exit
. But in either case, the solution -- import sys
is the same.
这是抱怨sys.argv而不是退出。但在任何一种情况下,解决方案 - import sys都是一样的。
#2
If you imported sys
then use sys.exit(0)
- change 0
with any result code you want to exit with. I had the same issue that before compilation when running from .py
file exit(0)
worked well but after compiling to .exe
it gave me an error. I had to change exit(0)
to sys.exit(0)
and it worked.
如果导入了sys,则使用sys.exit(0) - 使用要退出的任何结果代码更改0。我有同样的问题,在编译之前从.py文件exit(0)运行良好但编译到.exe后它给了我一个错误。我不得不将exit(0)更改为sys.exit(0)并且它工作正常。
#1
You need to import sys
first, since exit
(and argv
) is in that module.
您需要先导入sys,因为exit(和argv)位于该模块中。
The error I get when I run your code is:
我运行代码时遇到的错误是:
File "", line 1, in NameError: name 'sys' is not defined
NameError中的文件“”,第1行:名称'sys'未定义
which is complaining about sys.argv
instead of exit
. But in either case, the solution -- import sys
is the same.
这是抱怨sys.argv而不是退出。但在任何一种情况下,解决方案 - import sys都是一样的。
#2
If you imported sys
then use sys.exit(0)
- change 0
with any result code you want to exit with. I had the same issue that before compilation when running from .py
file exit(0)
worked well but after compiling to .exe
it gave me an error. I had to change exit(0)
to sys.exit(0)
and it worked.
如果导入了sys,则使用sys.exit(0) - 使用要退出的任何结果代码更改0。我有同样的问题,在编译之前从.py文件exit(0)运行良好但编译到.exe后它给了我一个错误。我不得不将exit(0)更改为sys.exit(0)并且它工作正常。