运行python脚本时如何抑制警告?

运行python脚本时如何抑制警告?,python,warnings,Python,Warnings,我正试图抑制警告 这是我的esri python版本 python-V Python 2.7.16 我试过这个 python.exe-W ignore GET_ESRIGIS_WEB_TOKEN.py 但它给出了这个错误 无效-忽略W选项:无效操作:““忽略” 我做错了什么 ps:这是帮助(请参阅下面的-W选项) -W参数:警告控制;参数是操作:消息:类别:模块:线路号 另外,PYTHONWARNINGS=arg usage: python.exe [option] ... [-c cmd |

我正试图抑制警告

这是我的esri python版本

python-V

Python 2.7.16

我试过这个

python.exe-W ignore GET_ESRIGIS_WEB_TOKEN.py

但它给出了这个错误

无效-忽略W选项:无效操作:““忽略”

我做错了什么

ps:这是帮助(请参阅下面的-W选项)

-W参数:警告控制;参数是操作:消息:类别:模块:线路号 另外,PYTHONWARNINGS=arg

usage: python.exe [option] ... [-c cmd | -m mod | file | -] [arg] ... Options and arguments (and corresponding environment variables): -b : issue warnings about comparing bytearray with unicode (-bb: issue errors) -B : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x -c cmd : program passed in as string (terminates option list) -d : debug output from parser; also PYTHONDEBUG=x -E : ignore PYTHON* environment variables (such as PYTHONPATH) -h : print this help message and exit (also --help) -i : inspect interactively after running script; forces a prompt even if stdin does not appear to be a terminal; also PYTHONINSPECT=x -m mod : run library module as a script (terminates option list) -O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x -OO : remove doc-strings in addition to the -O optimizations -R : use a pseudo-random salt to make hash() values of various types be unpredictable between separate invocations of the interpreter, as a defense against denial-of-service attacks -Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew -s : don't add user site directory to sys.path; also PYTHONNOUSERSITE -S : don't imply 'import site' on initialization -t : issue warnings about inconsistent tab usage (-tt: issue errors) -u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x see man page for details on internal buffering relating to '-u' -v : verbose (trace import statements); also PYTHONVERBOSE=x can be supplied multiple times to increase verbosity -V : print the Python version number and exit (also --version) -W arg : warning control; arg is action:message:category:module:lineno also PYTHONWARNINGS=arg -x : skip first line of source, allowing use of non-Unix forms of #!cmd -3 : warn about Python 3.x incompatibilities that 2to3 cannot trivially fix file : program read from script file - : program read from stdin (default; interactive mode if a tty) arg ...: arguments passed to program in sys.argv[1:] 用法:python.exe[选项]…[-c cmd |-m mod | file |-][arg]。。。 选项和参数(以及相应的环境变量): -b:发出有关将bytearray与unicode进行比较的警告 (-bb:问题错误) -B:不要在导入时写入.py[co]文件;还有pythontwritebytecode=x -c cmd:作为字符串传入的程序(终止选项列表) -d:从解析器调试输出;也可以是PYTHONDEBUG=x -E:忽略PYTHON*环境变量(例如PYTHONPATH) -h:打印此帮助消息并退出(也可以是--help) -i:在运行脚本后以交互方式进行检查;强制执行提示 如果stdin似乎不是终端,则PYTHONINSPECT=x -m mod:以脚本形式运行库模块(终止选项列表) -O:稍微优化生成的字节码;同时PYTHONOPTIMIZE=x -OO:除了-O优化之外,还要删除文档字符串 -R:使用伪随机salt使各种类型的hash()值 在解释器的不同调用之间不可预测,如 针对拒绝服务攻击的防御 -Q arg:分区选项:-Qold(默认),-Qwarn,-Qwarnall,-Qnew -s:不要将用户站点目录添加到sys.path;还要添加PYTHONNOUSERSITE -S:不要在初始化时暗示“导入站点” -t:发出有关选项卡使用不一致的警告(-tt:发出错误) -u:无缓冲二进制标准输出和标准输出;也是pythonunbuffer=x 有关'-u'的内部缓冲的详细信息,请参见手册页 -v:verbose(跟踪导入语句);也可以是PYTHONVERBOSE=x 可以多次提供以增加详细性 -V:打印Python版本号并退出(也是--version) -W参数:警告控制;参数为操作:消息:类别:模块:线路号 还有PYTHONWARNINGS=arg -x:跳过源代码的第一行,允许使用非Unix形式的#!cmd -3:警告2to3无法解决的Python 3.x不兼容问题 文件:从脚本文件读取的程序 -:从stdin读取的程序(默认;如果是tty,则为交互式模式) arg…:在sys.argv[1:]中传递给程序的参数
通常
python-W ignore file.py应该可以工作

但是,您也可以尝试将其添加到代码中以抑制所有警告

导入警告
警告。过滤器警告(“忽略”)

参考:


这很奇怪。
-W ignore
是正确的方法。如果可以安慰的话,它可以在我的python2 linux安装上运行!你有没有可能得到错误的
警告
模块?编写一个小脚本,导入并打印
警告。uu文件u
。你有
PYTHONWARNINGS
环境变量?嗯aps格式错误。也许这个“esri”python构建有些奇怪…不管是什么。这是一个课程作业吗?也许他们在构建中禁用了它? usage: python.exe [option] ... [-c cmd | -m mod | file | -] [arg] ... Options and arguments (and corresponding environment variables): -b : issue warnings about comparing bytearray with unicode (-bb: issue errors) -B : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x -c cmd : program passed in as string (terminates option list) -d : debug output from parser; also PYTHONDEBUG=x -E : ignore PYTHON* environment variables (such as PYTHONPATH) -h : print this help message and exit (also --help) -i : inspect interactively after running script; forces a prompt even if stdin does not appear to be a terminal; also PYTHONINSPECT=x -m mod : run library module as a script (terminates option list) -O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x -OO : remove doc-strings in addition to the -O optimizations -R : use a pseudo-random salt to make hash() values of various types be unpredictable between separate invocations of the interpreter, as a defense against denial-of-service attacks -Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew -s : don't add user site directory to sys.path; also PYTHONNOUSERSITE -S : don't imply 'import site' on initialization -t : issue warnings about inconsistent tab usage (-tt: issue errors) -u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x see man page for details on internal buffering relating to '-u' -v : verbose (trace import statements); also PYTHONVERBOSE=x can be supplied multiple times to increase verbosity -V : print the Python version number and exit (also --version) -W arg : warning control; arg is action:message:category:module:lineno also PYTHONWARNINGS=arg -x : skip first line of source, allowing use of non-Unix forms of #!cmd -3 : warn about Python 3.x incompatibilities that 2to3 cannot trivially fix file : program read from script file - : program read from stdin (default; interactive mode if a tty) arg ...: arguments passed to program in sys.argv[1:]