在python脚本中导入时隐藏警告消息

时间:2018-05-09 21:57:39

标签: python python-3.x pydicom

我想在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 

我该如何隐藏它?

1 个答案:

答案 0 :(得分:1)

这似乎对我有用:

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