How can I suppress the output to console when importing packages in RPy2 in Python?

StackOverflow https://stackoverflow.com/questions/4745388

  •  13-10-2019
  •  | 
  •  

Question

Whenever I run a script importing packages with import in RPy2 in Python, there are always some extra lines popping up in the console. I pasted in an example below. How can I suppress that behavior?

CookieJar:r cookies$ python script.py 

    ‘tseries’ version: 0.10-24

    ‘tseries’ is a package for time series analysis and computational
    finance.

    See ‘library(help="tseries")’ for details.
Was it helpful?

Solution

You could temporarily redirect the output stream to a blackhole just before the spammy peice of code.

import sys

class Blackhole(object):

    def write(self, string):
        pass

stdout = sys.stdout
sys.stdout = Blackhole()

function_el_spammo()

sys.stdout = stdout

OTHER TIPS

Besides require(tseries, quietly = TRUE) and using sink(), or its Python equivalent, there is also the simple

suppressMessages( library( tseries ))

which I prefer.

In your R script, I would preload the tseries package (just in case if its called by some other functio/package) using

require(tseries, quietly = TRUE)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top