How to ignore deprecation warnings in Python
Asked Answered
J

18

266

I keep getting this :

DeprecationWarning: integer argument expected, got float

How do I make this message go away? Is there a way to avoid warnings in Python?

Jadejaded answered 18/5, 2009 at 18:42 Comment(2)
When nothing else works: $ pip install shutup. Then at the top of the code import shutup;shutup.please(). This will disable all warnings.Schellens
341k views as of 2023-11-18 shows how bad this is. Why is it not a reported bug that it is so hard to switch these warning messages off once they were noticed?Herein
G
153

From documentation of the warnings module:

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

If you're on Windows: pass -W ignore::DeprecationWarning as an argument to Python. Better though to resolve the issue, by casting to int.

(Note that in Python 3.2, deprecation warnings are ignored by default.)

Glynnis answered 18/5, 2009 at 18:50 Comment(8)
I wish I could make this work... I get a /usr/bin/env: python -W ignore::DeprecationWarning: No such file or directory error. It works if I run python with the -W ignore::DeprecationWarning option on the command-line, but /usr/bin/env doesn't deal with it.Stunk
Seems to be a windows-only solution.Shark
You can set the env variable PYTHONWARNINGS this worked for me export PYTHONWARNINGS="ignore::DeprecationWarning:simplejson" to disable django json deprication warnings from sorlTrifoliate
@yvess, if this were an answer, I'd have voted for it. Seems a clean way to ignore specific warnings systemwide. I put it in my ~/.profile. Works great.Regelation
Hi can we some how turn this Deprecation Warning message to an message of type information. What I would like is just to display the message on the console not to be categorized as any type of warning.Colis
The chronology here is: for Python 2.6 DeprecationWarnings were on by default, from Python 2.7 to Python 3.2 DeprecationWarnings were off by default and for Python 3.7 they are back on by default! python.org/dev/peps/pep-0565Hurling
This won't work on Linux because you cannot pass multiple command line arguments to shebangs. #!/usr/bin/python -Wignore::DeprecationWarning should work, but it's preferable to use the warnings module.Psych
Using /usr/bin/env in the shebang just requires a -S, like so: #!/usr/bin/env -S python -W ignore::DeprecationWarningSubirrigate
W
321

You should just fix your code but just in case,

import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning) 
Wind answered 18/5, 2009 at 19:1 Comment(10)
Worked for me using iPythonAreca
This doesn't work for me at all, still seeing deprecationwarnings.Magocsi
@Magocsi I could be wrong but I think it matters where in your code you run warnings.filterwarnings("ignore", category=DeprecationWarning). I think you have to run this after you import the library that's spitting out the warnings, although I could be mistaken.Peraza
import warnings warnings.filterwarnings("ignore", category=RuntimeWarning)Denunciation
@CodingYourLife category is needed so you will still see other types of warnings like RuntimeWarning etc.Wind
@Magocsi Sometimes FutureWarnings are given in order to deprecate things...Pompeii
In my case, the code that was causing the warning was from xgboost import XGBClassifier. I had to put warnings.filterwarnings("ignore", category=DeprecationWarning) immediately before that import for it to work.Earwig
Works great to shut up the DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead warning when running unoconv (e. g. to convert a LibreOffice document to PDF). Just sudo vi /usr/bin/unoconv and insert those two lines. Tested on Ubuntu 23.04.Cristycriswell
This works! But the key is, this code has to be in the correct place. Moreover, the filterwarnings accepts a module name. I suppose one can specify the module the warning is coming from, but I haven't tested it though. More docs here: docs.python.org/3/library/warnings.html#warnings.filterwarningsPenicillate
Not working for me, still see LangChainDeprecationWarningLyon
J
242

I had these:

/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

Fixed it with:

import warnings

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

yourcode()

Now you still get all the other DeprecationWarnings, but not the ones caused by:

import md5, sha
Joanne answered 28/10, 2009 at 23:24 Comment(2)
Awesome, thank you so much!! (Took me a moment to realize I could also wrap non-import bits of code in this, since some packages were also generating DeprecationWarnings when used after import.) Very nice way to only silence specific DeprecationWarnings that I've already looked at and decided I want to ignore.Stunk
Works on WIndows and Linux, plus is completely independent of any parameters given to the executable. So works all the time. Additionally it only disables deprecation warnings for explicit libraries, so when the next ones get deprecated you still get informed to update your source code.Linkwork
G
153

From documentation of the warnings module:

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

If you're on Windows: pass -W ignore::DeprecationWarning as an argument to Python. Better though to resolve the issue, by casting to int.

(Note that in Python 3.2, deprecation warnings are ignored by default.)

Glynnis answered 18/5, 2009 at 18:50 Comment(8)
I wish I could make this work... I get a /usr/bin/env: python -W ignore::DeprecationWarning: No such file or directory error. It works if I run python with the -W ignore::DeprecationWarning option on the command-line, but /usr/bin/env doesn't deal with it.Stunk
Seems to be a windows-only solution.Shark
You can set the env variable PYTHONWARNINGS this worked for me export PYTHONWARNINGS="ignore::DeprecationWarning:simplejson" to disable django json deprication warnings from sorlTrifoliate
@yvess, if this were an answer, I'd have voted for it. Seems a clean way to ignore specific warnings systemwide. I put it in my ~/.profile. Works great.Regelation
Hi can we some how turn this Deprecation Warning message to an message of type information. What I would like is just to display the message on the console not to be categorized as any type of warning.Colis
The chronology here is: for Python 2.6 DeprecationWarnings were on by default, from Python 2.7 to Python 3.2 DeprecationWarnings were off by default and for Python 3.7 they are back on by default! python.org/dev/peps/pep-0565Hurling
This won't work on Linux because you cannot pass multiple command line arguments to shebangs. #!/usr/bin/python -Wignore::DeprecationWarning should work, but it's preferable to use the warnings module.Psych
Using /usr/bin/env in the shebang just requires a -S, like so: #!/usr/bin/env -S python -W ignore::DeprecationWarningSubirrigate
F
65

None of these answers worked for me so I will post my way to solve this. I use the following at the beginning of my main.py script and it works fine.


Use the following as it is (copy-paste it):

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

or 1-liner, thanks Philippe Remy 's comment

import warnings ; warnings.warn = lambda *args,**kwargs: None

Example:

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


import "blabla"
import "blabla"

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

Fishnet answered 24/5, 2018 at 23:49 Comment(8)
That worked when all other solutions didn't. Thanks!Foetus
This saved me too. Glad that I could help.Fishnet
Doesn't work in 3.7.3 for AstroPy deprecation warnings. :(Mossbunker
I've wrapped this solution in a lib: $ pip install shutup. Then at the top of the code import shutup;shutup.please(). This will disable all warnings.Schellens
Work for the whole application ! Thank you so muchAlake
Same. None of the other solutions worked. This is the only thing that worked.Donela
For those who want a one liner for this answer: warnings.warn = lambda *args, **kwargs: NoneDreiser
I work for me; though I have to put your code block on top of other import commandLyon
I
32

I found the cleanest way to do this (especially on windows) is by adding the following to C:\Python26\Lib\site-packages\sitecustomize.py:

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

Note that I had to create this file. Of course, change the path to python if yours is different.

Infuriate answered 4/3, 2010 at 17:35 Comment(0)
E
19

Docker Solution

  • Disable ALL warnings before running the python application
    • You can disable your dockerized tests as well
ENV PYTHONWARNINGS="ignore::DeprecationWarning"
Elbertelberta answered 2/5, 2019 at 7:41 Comment(0)
R
8

When you want to ignore warnings only in functions you can do the following.

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
    ...

Just add the @ignore_warnings decorator on the function you want to ignore all warnings

Rinna answered 4/12, 2017 at 21:2 Comment(0)
M
8

If you are using logging (https://docs.python.org/3/library/logging.html) to format or redirect your ERROR, NOTICE, and DEBUG messages, you can redirect the WARNINGS from the warning system to the logging system:

logging.captureWarnings(True)

It will capture the warnings with the tag "py.warnings". Also if you want to throw away those warnings without logging, you can then, set the logging level to ERROR by using:

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

It will cause all those warnings to be ignored without showing up in your terminal or anywhere else.

See https://docs.python.org/3/library/warnings.html and https://docs.python.org/3/library/logging.html#logging.captureWarnings and captureWarnings set to True doesn't capture warnings

In my case, I was formatting all the exceptions with the logging system, but warnings (e.g. scikit-learn) were not affected.

Meltage answered 17/9, 2020 at 22:17 Comment(1)
"logging.captureWarnings(True)" is what did it for me. This was the only way I could get boto3 to stop spitting out a million warnings that "datetime.datetime.utcfromtimestamp() is deprecated".Etna
G
7

Python 3

Just write below lines that are easy to remember before writing your code:

import warnings

warnings.filterwarnings("ignore")
Goodman answered 19/11, 2019 at 13:1 Comment(0)
R
5

Pass the correct arguments? :P

On the more serious note, you can pass the argument -Wi::DeprecationWarning on the command line to the interpreter to ignore the deprecation warnings.

Rizzi answered 18/5, 2009 at 18:49 Comment(0)
R
3

Convert the argument to int. It's as simple as

int(argument)
Repository answered 18/5, 2009 at 18:49 Comment(0)
D
3

For python 3, just write below codes to ignore all warnings.

from warnings import filterwarnings
filterwarnings("ignore")
Disclimax answered 14/12, 2019 at 16:34 Comment(0)
P
3

Try the below code if you're Using Python3:

import sys

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

or try this...

import warnings

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

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

or try this...

import warnings
warnings.filterwarnings("ignore")
Pastorship answered 8/3, 2020 at 4:53 Comment(0)
Y
0

To add to previous solutions, if you continue to receive warnings in certain parallel applications (such as those that use multiprocessing, it is possible that the warning filters need to be passed to child processes as well. You can do that with something like this:

import sys, os

if not sys.warnoptions:
    import warnings
    warnings.simplefilter("ignore")
    
    os.environ["PYTHONWARNINGS"] = "ignore"

Adding the os.environ line will cause subprocesses to also ignore warnings.

Yamen answered 11/12, 2023 at 19:39 Comment(0)
C
-1

If you know what you are doing, another way is simply find the file that warns you(the path of the file is shown in warning info), comment the lines that generate the warnings.

Cylix answered 16/8, 2019 at 7:33 Comment(2)
What if the warning comes from a third-party library?Friary
Generally, the lines that generate the warning are required for the program to function. Certainly a sub-optimal suggestion.Demimondaine
T
-2

A bit rough, but it worked for me after the above methods did not.

./myscrypt.py 2>/dev/null

Tollgate answered 4/11, 2022 at 14:35 Comment(0)
F
-3

Not to beat you up about it but you are being warned that what you are doing will likely stop working when you next upgrade python. Convert to int and be done with it.

BTW. You can also write your own warnings handler. Just assign a function that does nothing. How to redirect python warnings to a custom stream?

Flatways answered 18/5, 2009 at 18:55 Comment(1)
That advice only works if it is indeed his own code and not from some 3rd party package.Nikolenikoletta
P
-10

Comment out the warning lines in the below file:

lib64/python2.7/site-packages/cryptography/__init__.py
Pain answered 20/9, 2020 at 8:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.