NameError:未定义名称“退出” [英] NameError: name 'exit' is not defined

查看:91
本文介绍了NameError:未定义名称“退出”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用cxfreeze从planrequest.py创建Windows可执行文件。看来工作正常,但是当我运行exe文件时,出现 NameError:未定义名称'exit'



名称出口未在python中定义指出已解决此问题使用 import sys 。但是,我使用import sys。该代码可以作为python脚本正常运行(例如,在编译为可执行文件之前,我已经广泛测试了命令行参数。)

 如果len(sys.argv)== 1,则导入套接字
导入sys


print( Usage:)
print( PlanRequest [Request String] [服务器IP(可选:如果省略,则假定为127.0.0.1)])
exit()

#[处理请求]


解决方案

导入sys不足以退出 生活在全球范围内。



您要么需要做

 从sys import出口
exit()

 导入系统
sys.exit()

请注意,因为您还使用了argv,在第一种情况下,您应该 从sys import argv,exit p>

I used cxfreeze to create a Windows executable from planrequest.py. It seemed to work ok, but when I run the exe file I get NameError: name 'exit' is not defined

name exit is not defined in python states that the fix is to use import sys. However, I use import sys. The code runs fine as a python script (as in, I extensively tested the command line arguments before compiling to an executable.)

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.

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

这篇关于NameError:未定义名称“退出”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆