IT

Python에서 권장 해제 경고를 무시하는 방법

itgroup 2023. 1. 1. 11:11
반응형

Python에서 권장 해제 경고를 무시하는 방법

계속 이런 생각이 들어요.

DeprecationWarning: integer argument expected, got float

이 메시지를 지우려면 어떻게 해야 하나요?Python에서 경고를 피할 수 있는 방법이 있나요?

그냥 코드를 고쳐야 하는데 혹시 모르니까

import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning) 

이런 게 있었어요.

/home/eddyp/virtualenv/lib/python2.6/site-packages/Twisted-8.2.0-py2.6-linux-x86_64.egg/twisted/persisted/sob.py:12:
DeprecationWarning: the md5 module is deprecated; use hashlib instead import os, md5, sys

/home/eddyp/virtualenv/lib/python2.6/site-packages/Twisted-8.2.0-py2.6-linux-x86_64.egg/twisted/python/filepath.py:12:
DeprecationWarning: the sha module is deprecated; use the hashlib module instead import sha

수정 내용:

import warnings

with warnings.catch_warnings():
    warnings.filterwarnings("ignore",category=DeprecationWarning)
    import md5, sha

yourcode()

이제 넌 다른 모든 걸 가질 수 있어DeprecationWarnings. 단, 다음 원인으로 인한 것은 아닙니다.

import md5, sha

모듈 설명서에서 다음 내용을 참조하십시오.

 #!/usr/bin/env python -W ignore::DeprecationWarning

Windows 를 사용하고 있는 경우: 패스-W ignore::DeprecationWarningPython에 대한 인수입니다.그래도 int에 캐스팅하여 문제를 해결하는 것이 좋습니다.

(Python 3.2에서는 디폴트로 권장 해제 경고는 무시됩니다.)

이 답변들 중 어느 것도 통하지 않았으므로 이 문제를 해결하기 위해 제 방법을 게시하겠습니다.다음을 사용합니다.at the beginning of my main.py스크립트가 제대로 작동합니다.


다음을 그대로 사용합니다(복사 붙여넣기).

def warn(*args, **kwargs):
    pass
import warnings
warnings.warn = warn

예:

import "blabla"
import "blabla"

def warn(*args, **kwargs):
    pass
import warnings
warnings.warn = warn

# more code here...
# more code here...

이것을 실시하기 위한 가장 깨끗한 방법(특히 Windows 에서는)은, C 에 다음의 항목을 추가하는 것입니다.\Python26\Lib\site-packages\sitecustomize.py:

import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)

이 파일을 작성해야 했습니다.물론 경로가 다를 경우 python으로 변경합니다.

도커 솔루션

  • python 응용 프로그램을 실행하기 전에 모든 경고 사용 안 함
    • 도커라이즈된 테스트를 비활성화할 수도 있습니다.
ENV PYTHONWARNINGS="ignore::DeprecationWarning"

파이썬 3

코드를 작성하기 전에 기억하기 쉬운 행을 아래에 적습니다.

import warnings

warnings.filterwarnings("ignore")

올바른 인수를 전달하시겠습니까?:P

좀 더 진지하게 말하면, 인수 -Wi::를 전달할 수 있습니다.권장 해제 경고를 무시하기 위해 인터프리터에 대한 명령줄 경고.

기능에서만 경고를 무시하려는 경우 다음을 수행할 수 있습니다.

import warnings
from functools import wraps


def ignore_warnings(f):
    @wraps(f)
    def inner(*args, **kwargs):
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("ignore")
            response = f(*args, **kwargs)
        return response
    return inner

@ignore_warnings
def foo(arg1, arg2):
    ...
    write your code here without warnings
    ...

@ignore_warnings
def foo2(arg1, arg2, arg3):
    ...
    write your code here without warnings
    ...

모든 경고를 무시하는 함수에 @ignore_warnings 데코레이터를 추가합니다.

인수를 int로 변환합니다.처럼 간단하다

int(argument)

로깅(https://docs.python.org/3/library/logging.html)을 사용하여 ERROR, NOTICE 및 DEBUG 메시지를 포맷 또는 리다이렉트하는 경우 경고 시스템에서 로깅 시스템으로 리다이렉트할 수 있습니다.

logging.captureWarnings(True)

"py.warnings"라는 태그가 붙은 경고를 캡처합니다.또한 이러한 경고를 로깅 없이 폐기할 경우 다음을 사용하여 로깅 수준을 ERROR로 설정할 수 있습니다.

logging.getLogger("py.warnings").setLevel(logging.ERROR)

단말기나 다른 곳에 나타나지 않고 모든 경고가 무시됩니다.

https://docs.python.org/3/library/warnings.html 및 https://docs.python.org/3/library/logging.html#logging.captureWarnings을 참조하고 True로 설정된 captureWarnings는 경고를 캡처하지 않습니다.

내 경우 로그 시스템에서 모든 예외를 포맷하고 있었지만 경고(skikit-learn 등)는 영향을 받지 않았습니다.

python 3의 경우 아래에 코드를 작성하여 모든 경고를 무시합니다.

from warnings import filterwarnings
filterwarnings("ignore")

Python3 를 사용하고 있는 경우는, 다음의 코드를 사용해 주세요.

import sys

if not sys.warnoptions:
    import warnings
    warnings.simplefilter("ignore")

아니면 이걸...

import warnings

def fxn():
    warnings.warn("deprecated", DeprecationWarning)

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    fxn()

아니면 이걸...

import warnings
warnings.filterwarnings("ignore")

작업 내용을 알고 있는 경우, 다른 방법은 경고하는 파일을 찾아서(파일 경로가 경고 정보에 표시됨), 경고를 생성하는 행에 주석을 다는 것입니다.

이 문제로 당신을 때리는 것은 아니지만, 당신이 하고 있는 일이 다음 번에 python을 업그레이드 할 때 작동을 멈출 수 있다는 경고를 받고 있습니다.int로 변환하여 종료합니다.

그나저나 경고 핸들러를 직접 작성할 수도 있습니다.아무 것도 하지 않는 함수를 할당하기만 하면 됩니다.python 경고를 사용자 지정 스트림으로 리디렉션하려면 어떻게 해야 합니다.

다음 파일의 경고 행을 코멘트 아웃합니다.

lib64/python2.7/site-packages/cryptography/__init__.py

언급URL : https://stackoverflow.com/questions/879173/how-to-ignore-deprecation-warnings-in-python

반응형