禁用jupyter笔记本中的警告

时间:2018-02-16 14:24:27

标签: python jupyter-notebook

我在jupyter笔记本中收到了这个警告。

/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:10: DeprecationWarning: object of type <class 'float'> cannot be safely interpreted as an integer.
  # Remove the CWD from sys.path while we load stuff.
/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:11: DeprecationWarning: object of type <class 'float'> cannot be safely interpreted as an integer.
  # This is added back by InteractiveShellApp.init_path()

这很烦人,因为它出现在我做的每一次跑步中:

enter image description here

如何修复或停用它?

4 个答案:

答案 0 :(得分:7)

如果您确定您的代码正确且简单,则想消除此警告以及笔记本中的所有其他警告,请执行以下操作:

import warnings
warnings.filterwarnings('ignore')

答案 1 :(得分:2)

您还可以仅针对某些代码行取消警告:

import warnings

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

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    function_that_warns()  # this will not show a warning

答案 2 :(得分:1)

如果将参数作为float传递,则会收到此警告。该参数应为整数。

,例如,在下面的例子中,num应该是一个整数,但是作为float传递:

import numpy as np
np.linspace(0, 10, num=3.0)

这会打印出你收到的警告:

ipykernel_launcher.py:2: DeprecationWarning: object of type <class 'float'> cannot be safely interpreted as an integer.

由于代码的某些部分缺失,我无法弄清楚,哪个参数应该作为整数传递,但以下示例说明了如何解决此问题:

import numpy as np
np.linspace(0, 10, num=int(3.0))

答案 3 :(得分:1)

尝试一下:

import warnings
warnings.filterwarnings('ignore')
warnings.simplefilter('ignore')