The documentationは言う:my_sub_program.py
の
Exceptions raised in the child process, before the new program has started to execute, will be re-raised in the parent. Additionally, the exception object will have one extra attribute called child_traceback, which is a string containing traceback information from the child’s point of view.
内容:my_main_program.py
の
raise Exception("I am raised!")
内容:私はmy_main_program.py
を実行すると、私は次のエラーを取得する
import sys
import subprocess
try:
subprocess.check_output([sys.executable, "my_sub_program.py"])
except Exception as e:
print e.child_traceback
:
Traceback (most recent call last):
File "my_main_program.py", line 6, in <module>
print e.child_traceback
AttributeError: 'CalledProcessError' object has no attribute 'child_traceback'
どのように私は、サブプロセス・プログラム・コードを変更せずにサブプロセスのトレースバックにアクセスすることができますか?これは、サブプログラムコード全体に大きなtry/except
句を追加することを避けたいのではなく、メインプログラムからのエラーログを処理することを意味します。
編集:sys.executable
は、メインプログラムを実行するインタープリタとは異なるインタープリタで置き換える必要があります。
ドクは「新しいプログラムが実行を開始する前に、」新しいプログラムが実行された一方で、あなたのケースで例外が、それ故に何の 'child_traceback'を提起しなかったと言います。新しいプログラムが実行されると、 'CalledProcessError'例外をキャッチして、次のようにする必要があります:http://stackoverflow.com/questions/24849998/how-to-catch-exception-output-from-python-subprocess-check -output 'CalledProcessError.output'を使用した出力 – mguijarr
私の例では、 'CalledProcessError.output'は標準出力だけをキャプチャしましたが、Exceptionのトレースバックはキャプチャしませんでした。 – schreon
これは、出力が 'stderr'で送られたためです。 – mguijarr