Reputation: 129
I want to "debug" my pyomo model. The output of the model.pprint()
method looks helpful but it is too long so the console only displays and stores the last lines. How can I see the first lines. And how can I store this output in a file
(I tried pickle, json, normal f.write
but since the output of .pprint()
is of type NONE
I wasn't sucessfull until now. (I am also new to python and learning python and pyomo in parallel).
None of this works : '''
with open('some_file2.txt', 'w') as f:
serializer.dump(x, f)
import pickle
object = Object()
filehandler = open('some_file', 'wb')
pickle.dump(x, filehandler)
x = str(instance)
x = str(instance.pprint())
f = open('file6.txt', 'w')
f.write(x)
f.write(instance.pprint())
f.close()
Upvotes: 1
Views: 5317
Reputation: 95
For me the accepted answer does not work, pprint has a different signature.
help(instance.pprint)
pprint(ostream=None, verbose=False, prefix='') method of pyomo.core.base.PyomoModel.ConcreteModel instance
# working for me:
with open(path, 'w') as output_file:
instance.pprint(output_file)
Upvotes: 4
Reputation: 8277
instance.pprint()
prints in the console (stdout for standard output), but does not return the content (the return is None
as you said). To have it print in a file, you can try to redirect the standard output to a file.
Try:
import sys
f = open('file6.txt', 'w')
sys.stdout = f
instance.pprint()
f.close()
It looks like there is a cleaner solution from Bethany =)
Upvotes: 0
Reputation: 2828
Use the filename
keyword argument to the pprint
method:
instance.pprint(filename='foo.txt')
Upvotes: 4