8
votes

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
Just import sys, like this: from sys import exit; import sysfelipsmartins
you're using sys.argv, so clearly, you've imported sys. So just do sys.exitinspectorG4dget
A quick question. I run it earlier with a super computer, and it works. But today I run it in my PC with canopy, it gives me an error like that. Does anyone know why ?study

2 Answers

6
votes

You need to import sys first, since exit (and argv) is in that module.

The error I get when I run your code is:

File "", line 1, in NameError: name 'sys' is not defined

which is complaining about sys.argv instead of exit. But in either case, the solution -- import sys is the same.

4
votes

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.