Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

name exit is not defined in python

Tags:

python

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)
like image 304
study Avatar asked May 09 '15 20:05

study


2 Answers

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.

like image 78
khampson Avatar answered Nov 11 '22 02:11

khampson


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.

like image 5
user3760296 Avatar answered Nov 11 '22 02:11

user3760296