2016-05-28 10 views
-1

これらは私のファイルです:私は全くそれにアクセスすることはできませんのでPythonモジュールからカスタム例外ハンドラにアクセスする方法は?

myModule/__init__.py 

import sys 

class MyModule: 
    handle_exceptions = handle_exceptions 
    def __init__(self): 
    sys.excepthook = handle_exceptions 

#myModule/handle_exceptions.py 
def handle_exceptions(ex_cls, ex, tb): 
    #not important here  
    pass 


#my_script.py 
import sys 
import myModule 
sys.excepthook = myModule.handle_exceptions 

「handle_exceptions」の内容がここでは重要ではありません。

Traceback (most recent call last): 
File "my_script.py", line 2, in <module> 
    import myModule 
File "D:\Python34\myModule\__init__.py", line 3, in <module> 
    class MyModule: 
File "D:\Python34\myModule\__init__.py", line 4, in MyModule 
    handle_exceptions = handle_exceptions 
NameError: name 'handle_exceptions' is not defined 
+0

を試してみてください。 self.handle_exceptions = handle_exceptions –

+0

@VikasMadhusudana:あなたの提案で同じ質問を更新しました。 – rsk82

+0

@VikasMadhusudana:質問が更新されましたが、再び動作しません。 – rsk82

答えて

0

あなたがのmymoduleのメンバーhandle_exceptionsを持っている必要があり、この

myModule/__init__.py 
import sys 

class MyModule: 
    def __init__(self): 
    sys.excepthook = handle_exceptions 
    self.handle_exceptions = handle_exceptions 


#myModule/handle_exceptions.py 
def handle_exceptions(ex_cls, ex, tb): 
    #not important here 
    pass 




import sys 
from myModule import handle_exceptions 
sys.excepthook = handle_exceptions 
関連する問題