这些是我的文件:
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 :(得分:0)
试试这个
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