【问题标题】:Hide warning message on import in python script在python脚本中隐藏导入警告消息
【发布时间】:2018-05-09 21:57:38
【问题描述】:

我想在 python 脚本中导入 pydicom。因此,我使用以下命令执行此操作而没有任何警告消息:

import sys, warnings
if not sys.warnoptions:
   warnings.simplefilter("ignore")
   try:
       import pydicom as dicom
   except ImportError:
       import dicom

但我收到以下警告信息!!!!

/Users/anaconda3/lib/python3.6/site-packages/dicom/__init__.py:53: UserWarning: 
This code is using an older version of pydicom, which is no longer 
maintained as of Jan 2017.  You can access the new pydicom features and API 
by installing `pydicom` from PyPI.
See 'Transitioning to pydicom 1.x' section at pydicom.readthedocs.org 
for more information.
warnings.warn(msg)
/Users/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters 

如何隐藏它?

【问题讨论】:

  • 您是否有任何特定于 Python 2 的问题需要标记此 Python 2?您似乎没有使用 Python 2。
  • 我使用 python 3,这篇文章是针对 python 3 的,但我也遇到了 python 2 的问题。

标签: python python-3.x pydicom


【解决方案1】:

也许-> python -W 忽略archive.py

选项 -W 跳过警告

【讨论】:

    【解决方案2】:

    这似乎对我有用:

    >>> import warnings
    >>> try:
    ...     import pydicom as dicom
    ... except ImportError:
    ...     with warnings.catch_warnings():
    ...         warnings.simplefilter("ignore")
    ...         import dicom
    ... 
    >>> 
    

    【讨论】:

    • 我在我的 python3 脚本中使用了它,但它还没有工作!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多