"모듈을 찾을 수없는 모듈"과 수입업자의 "모듈 예외"와 어떻게 구별합니까?
-
13-09-2019 - |
문제
파이썬에서 import does_not_exist
제기 ImportError
, 그리고
import exists
exists.py
:
import does_not_exist
또한 올릴 것입니다 ImportError
.
코드의 차이를 어떻게 말해야합니까?
해결책
내가 아는 유일한 방법은 Toplevel Modulename "Evelists"가 예외 메시지에 있는지 확인하는 것입니다.
try:
import exists
except ImportError as exc:
if "exists" in str(exc):
pass
else:
raise
이것이 Python의 수입업자에 대한 기능 요청 일 수 있습니까? 모듈 이름에 대한 변수를 갖는 것은 확실히 편리합니다 ..
다른 팁
Traceback의 TB_NEXT를 사용할 수 있습니다. 다른 모듈에서 예외가 발생하면 아무도 다릅니다.
import sys
try:
import exists
except Exception, e:
print "None on exists", sys.exc_info()[2].tb_next == None
try:
import notexists
except Exception, e:
print "None on notexists", sys.exc_info()[2].tb_next == None
>>> None on exists False
>>> None on notexists True
제휴하지 않습니다 StackOverflow