为什么我看不到 Numpy 的 DeprecationWarning?

问题描述 投票:0回答:2

我最近在我的一台机器上更新了 Python 的 Numpy 包,显然我已经依赖 numpy 的已弃用功能有一段时间了:

>>> np.__version__
'1.10.4'
>>> a = np.ones(10, dtype=np.uint16)
>>> a /= 0.5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ufunc 'true_divide' output (typecode 'd') could not be coerced to provided output parameter (typecode 'H') according to the casting rule ''same_kind''

上述链接中的一位评论者指出:

可能意味着您永远没有看到弃用警告 ;)

...这是正确的,我没有。

但是为什么?我怎么错过了弃用警告?

文档一致,相同的代码在我之前的numpy版本中的工作方式有所不同:

>>> np.__version__
'1.9.2'
>>> a = np.ones(10, dtype=np.uint16)
>>> a /= 0.5
>>> a
array([2, 2, 2, 2, 2, 2, 2, 2, 2, 2], dtype=uint16)

...但是这不应该触发警告吗?我是否误解了 numpy 如何处理弃用警告? 我如何确定我没有错过其他弃用警告

我的python环境:

Python 3.5.1 |Anaconda 4.0.0 (64-bit)| (default, Feb 16 2016, 09:49:46) [MSC v.1900 64 bit (AMD64)] on win32
python numpy warnings deprecated deprecation-warning
2个回答
9
投票

弃用警告默认被忽略。您需要通过使用

-Wd
标志:

运行 Python 来启用它们
python -Wd my_source_file.py

或者通过安装新的警告过滤器规范来覆盖忽略 DeprecationWarning 的规范:

import warnings

# Print any warning the first time a given source line issues them,
# overriding built-in filters that ignore some warning types.
warnings.filterwarnings("default")

0
投票

我正在运行 python3 --version
Python 3.8.16 Ansible --版本
ansible [核心 2.13.13]

我无法在屏幕上显示弃用警告。我有

要禁用这些警告,请将以下值设置为 False:

deprecation_warnings=True 。这不会生效 true/false

© www.soinside.com 2019 - 2024. All rights reserved.