223

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?

kaya3
  • 41,043
  • 4
  • 50
  • 79
Mohammed
  • 2,403
  • 2
  • 16
  • 8
  • 4
    When nothing else works: `$ pip install shutup`. Then at the top of the code `import shutup;shutup.please()`. This will disable all warnings. – polvoazul Jul 01 '21 at 13:31

16 Answers16

262

You should just fix your code but just in case,

import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning) 
ismail
  • 43,610
  • 8
  • 84
  • 94
  • 2
    Worked for me using iPython – zbinsd Sep 12 '13 at 20:31
  • 49
    This doesn't work for me at all, still seeing deprecationwarnings. – user1244215 Oct 22 '13 at 00:43
  • 13
    @user1244215 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. – Jack Kelly May 23 '14 at 20:57
  • import warnings warnings.filterwarnings("ignore", category=RuntimeWarning) – Kevin Parker Jul 24 '14 at 17:37
  • 1
    @CodingYourLife category is needed so you will still see other types of warnings like RuntimeWarning etc. – ismail Jan 28 '17 at 08:39
  • 1
    @user1244215 Sometimes `FutureWarnings` are given in order to deprecate things... – user27182 Apr 10 '17 at 10:35
  • 4
    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. – sedeh Jul 24 '17 at 06:36
215

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
phoenix
  • 5,738
  • 3
  • 33
  • 41
Eddy Pronk
  • 6,269
  • 5
  • 31
  • 54
  • 3
    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. – weronika Oct 04 '11 at 18:47
  • 2
    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. – Maddes Jan 08 '22 at 22:54
139

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

0 _
  • 9,294
  • 10
  • 71
  • 105
Stephan202
  • 57,780
  • 13
  • 124
  • 131
  • 9
    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. – weronika Oct 04 '11 at 18:34
  • 5
    Seems to be a windows-only solution. – Daniel Miles Dec 16 '11 at 19:38
  • 18
    You can set the env variable PYTHONWARNINGS this worked for me `export PYTHONWARNINGS="ignore::DeprecationWarning:simplejson"` to disable django json deprication warnings from sorl – yvess Feb 13 '14 at 16:56
  • @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. – aljabear May 16 '14 at 15:17
  • 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. – Krishna Oza Feb 25 '16 at 10:21
  • The chronology here is: for Python 2.6 `DeprecationWarning`s were on by default, from Python 2.7 to Python 3.2 `DeprecationWarning`s were off by default and for Python 3.7 they are back on by default! https://www.python.org/dev/peps/pep-0565/ – Chris_Rands Feb 06 '18 at 18:57
  • 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. – nyuszika7h Aug 12 '19 at 15:28
52

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

Example:

import "blabla"
import "blabla"

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

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

seralouk
  • 27,314
  • 8
  • 98
  • 117
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.

Tristan Havelick
  • 63,483
  • 19
  • 53
  • 64
8

Docker Solution

  • Disable ALL warnings before running the python application
    • You can disable your dockerized tests as well
ENV PYTHONWARNINGS="ignore::DeprecationWarning"
Asclepius
  • 49,954
  • 14
  • 144
  • 128
Marcello de Sales
  • 19,543
  • 14
  • 70
  • 75
7

Python 3

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

import warnings

warnings.filterwarnings("ignore")
Dipen Gajjar
  • 1,110
  • 13
  • 21
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.

shylent
  • 9,966
  • 6
  • 36
  • 54
4

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

Trideep Rath
  • 3,383
  • 21
  • 13
3

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

from warnings import filterwarnings
filterwarnings("ignore")
Ashish Anand
  • 2,209
  • 19
  • 14
3

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

int(argument)
Gonzalo Quero
  • 3,300
  • 17
  • 23
2

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")
Amar Kumar
  • 1,880
  • 2
  • 19
  • 32
2

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)

See https://docs.python.org/3/library/warnings.html and https://docs.python.org/3/library/logging.html#logging.captureWarnings

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

ruloweb
  • 534
  • 6
  • 9
0

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.

Statham
  • 3,742
  • 2
  • 30
  • 44
-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?

Community
  • 1
  • 1
SpliFF
  • 37,036
  • 16
  • 86
  • 116
-6

Comment out the warning lines in the below file:

lib64/python2.7/site-packages/cryptography/__init__.py
DRPK
  • 1,969
  • 1
  • 13
  • 25