简体   繁体   English

NameError:未定义名称“退出”

[英]NameError: name 'exit' is not defined

I used cxfreeze to create a Windows executable from planrequest.py.我使用 cxfreeze 从 planrequest.py 创建了一个 Windows 可执行文件。 It seemed to work ok, but when I run the exe file I get NameError: name 'exit' is not defined它似乎工作正常,但是当我运行 exe 文件时,我得到NameError: name 'exit' is not defined

name exit is not defined in python states that the fix is to use import sys . name exit is not defined in python states that the fix is to use import sys However, I use import sys.但是,我使用 import sys. The code runs fine as a python script (as in, I extensively tested the command line arguments before compiling to an executable.)该代码作为 python 脚本运行良好(例如,在编译为可执行文件之前,我广泛测试了命令行参数。)

import socket
import sys

if len(sys.argv) == 1:
    print("Usage:")
    print("PlanRequest [Request String] [Server IP (optional: assumes 127.0.0.1 if omitted)]")
    exit()

#[do stuff with the request]

Importing sys will not be enough to make exit live in the global scope.导入 sys 不足以使exit在全局范围内生效。

You either need to do你要么需要做

from sys import exit
exit()

or或者

import sys
sys.exit()

Note that, as you are also using argv, in the first case you should do from sys import argv,exit请注意,由于您也在使用 argv,在第一种情况下,您应该from sys import argv,exit

You have to apply the function to sys:您必须将该函数应用于 sys:

from sys import exit
exit()

because exit is the function itself, you need to call it with ()因为exit是函数本身,需要用()调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM