フィルターのクリア

python from 2014b matlab - debug challanges - where is python stdout, stderr?

1 回表示 (過去 30 日間)
Chris Barnhart
Chris Barnhart 2015 年 1 月 18 日
コメント済み: Chris 2015 年 1 月 21 日
When running python from 2014b matlab, where does the output from the python program go?
The greater problem is I'm at a loss as to how to debug my python script. It runs well outside of matlab, but has started to fail when run inside matlab in an seemingly un-tracable manner as it has grown. I've added extra exception handlers, but can't see the output.
except :
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
traceback.print_exception(exc_type, exc_value, exc_traceback, limit=5, file=sys.stdout)
(I'll change that file=sys.stdout to a real file which can help)
Any debug tips, or where I can find stdout?

採用された回答

Robert Snoeberger
Robert Snoeberger 2015 年 1 月 20 日
編集済み: Robert Snoeberger 2015 年 1 月 20 日
sys.stdout should be redirected to the MATLAB command window. Do you see output with Python's print function?
Example
>> py.print('hello!')
hello!
>>
sys.stderr is not redirected. When you use the functions print_tb and print_exception, you need to tell the functions to write to sys.stdout.
As a debug tip, the exception you catch in MATLAB due to a Python error is a PyException. The PyException has a property ExceptionObject, which is the same result you get from calling sys.exc_info.
>> try
py.fractions.Fraction(1,0)
catch e
end
>> e
e =
PyException with properties:
ExceptionObject: [1x1 py.tuple]
identifier: 'MATLAB:Python:PyException'
message: 'Python Error: both arguments should be Rational instances'
cause: {}
stack: [0x1 struct]
>> e.ExceptionObject
ans =
Python tuple with no properties.
(<type 'exceptions.TypeError'>, TypeError('both arguments should be Rational instances',), <traceback object at 0x0000000012AE9A48>)
>> py.traceback.extract_tb(py.operator.getitem(e.ExceptionObject, int32(2)))
ans =
Python list with no properties.
[('C:\\Python27\\lib\\fractions.py', 158, '__new__', 'raise TypeError("both arguments should be "')]
>>
  3 件のコメント
Robert Snoeberger
Robert Snoeberger 2015 年 1 月 21 日
Debugging will be very difficult without a valid matlab.exception.PyException class. The message says that matlab.exception.PyException contains a parse error or cannot be found. Use the which function to see if can be found.
>> which matlab.exception.PyException
C:\Program Files\MATLAB\R2014b\toolbox\matlab\external\interfaces\python\+matlab\+exception\PyException.m % matlab.exception.PyException constructor
>>
If it is found, then try to create a PyException to check for a parse error.
>> e = matlab.exception.PyException('MATLAB:Py:Test', 'testing', [])
e =
PyException with properties:
ExceptionObject: []
identifier: 'MATLAB:Py:Test'
message: 'testing'
cause: {}
stack: [0x1 struct]
>>
Chris Barnhart
Chris Barnhart 2015 年 1 月 21 日
"Debugging will be very difficult without a valid matlab.exception.PyException class. " It is very difficult!
PyException wasn't found, so I added 'C:\Program Files\MATLAB\R2014b\toolbox\matlab\external' and subfolders with pathtool. Your fraction exception now works. Thank you.
Wonder if some other config issue can explain the py.print() issue....

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by