【问题标题】:Disable warnings in jupyter notebook在 jupyter notebook 中禁用警告
【发布时间】:2018-07-27 11:36:50
【问题描述】:

我在 jupyter notebook 中收到此警告。

/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()

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

如何修复或禁用它?

【问题讨论】:

标签: python jupyter-notebook


【解决方案1】:

如果您将参数作为浮点数传递,您将收到此警告,该参数应为整数。

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

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

【讨论】:

    【解决方案2】:

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

    import warnings
    warnings.filterwarnings('ignore')
    

    【讨论】:

    【解决方案3】:

    试试这个:

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

    【讨论】:

      【解决方案4】:

      您还可以仅针对某些代码行禁止显示警告:

      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
      

      【讨论】:

        【解决方案5】:

        如果您来自 google,这些答案都不起作用,我终于找到了解决方案 (credit to this answer)

        from IPython.utils import io
        
        with io.capture_output() as captured:
            foo()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-06-07
          • 1970-01-01
          • 2020-08-04
          • 2019-12-28
          • 1970-01-01
          • 2020-09-12
          • 1970-01-01
          • 2018-10-23
          相关资源
          最近更新 更多