Search code examples
pythonpyomo

how to make pyomo silent (verbosity 0) when solving a model


I'm running batch of pyomo solver with different conditions and I would like to make the solver silent.

opt = SolverFactory('glpk') 
instance = model.create_instance(dat_file)
results = opt.solve(instance, tee=True, timelimit=300)

In other word, when running this last line, I don't want anything printed on the stdout. Is it possible ?

A work around solution I used is to redirect stdout:

from contextlib import redirect_stdout

opt = SolverFactory('glpk')
instance = model.create_instance(dat_file)
with open("log.txt", mode='w', encoding='utf-8') as fp:
    with redirect_stdout(fp):
        results = opt.solve(instance, tee=True, timelimit=300)

But I wonder if there is a "cleaner" solution ?


Solution

  • I believe you can simply set the option tee=False when you solve the model to suppress all solver output.