我有一个Python脚本,它使用我的雇主提供的一些闭箱Python函数(即我无法编辑这些函数).当我调用这些函数时,它们正在打印输出到我想要压制的linux终端.我尝试过重定向stdout/stderr;
orig_out = sys.stdout
sys.stdout = StringIO()
rogue_function()
sys.stdout = orig_out
Run Code Online (Sandbox Code Playgroud)
但这没有抓住输出.我认为我通过Python调用的函数(上面的rogue_function())实际上是编译C代码的包装器,它们实际上正在进行打印.
有没有人知道我可以通过函数(以及函数调用的任何子函数)对stdout/stderr的任何打印进行"深度捕获"?
更新:
我最终采用了下面选定答案中概述的方法并编写了一个上下文管理器来压制stdout和stderr:
# Define a context manager to suppress stdout and stderr.
class suppress_stdout_stderr(object):
'''
A context manager for doing a "deep suppression" of stdout and stderr in
Python, i.e. will suppress all print, even if the print originates in a
compiled C/Fortran sub-function.
This will not suppress raised exceptions, since exceptions are printed
to stderr just before a script exits, and after the context manager …Run Code Online (Sandbox Code Playgroud) 我使用以下命令将带有GPU支持的Tensorflow和Keras安装到Anaconda(v1.6.5)中的环境中:
conda install -n EnvName tensorflow-gpu
conda install -n EnvName -c conda-forge keras-gpu
Run Code Online (Sandbox Code Playgroud)
我的机器上安装了NVIDIA Quadro 2200K,驱动程序为v384.66,cuda-8.0,cudnn 7.0
当我在训练阶段尝试使用Keras运行python代码时,我得到以下内容
英特尔MKL错误:进入DLASCL时参数4不正确.
然后
文件"/home/User/anaconda3/envs/keras_gpu/lib/python3.6/site-packages/numpy/linalg/linalg.py",第99行,在_raise_linalgerror_svd_nonconvergence中引发LinAlgError("SVD没有收敛")numpy.linalg .linalg.LinAlgError:SVD没有收敛
其他相关消息来源建议检查NaN和Infs的数据,但我的数据肯定是干净的.顺便说一句,CPU版本的安装工作正常,只有在尝试在GPU上运行时才会出现问题
我试图重新安装Anaconda,重新安装CUDA和numpy,但它没有成功.