Created
July 8, 2020 07:46
-
-
Save waszil/fccda8749ed5dd5baa336bae8e88dd7a to your computer and use it in GitHub Desktop.
Disable traceback printing temporarily in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from contextlib import contextmanager | |
@contextmanager | |
def hide_traceback() -> None: | |
def is_running_from_ipython() -> bool: | |
""" Checks whether running in IPython interactive console or not """ | |
try: | |
import IPython | |
except ImportError: | |
return False | |
try: | |
from IPython import get_ipython | |
except ImportError: | |
return False | |
else: | |
return get_ipython() is not None | |
# noinspection PyBroadException | |
try: | |
yield | |
except Exception: | |
if is_running_from_ipython(): | |
import IPython | |
IPython.core.getipython.get_ipython().showtraceback(exception_only=True) | |
else: | |
etype, value, tb = sys.exc_info() | |
print(f"{etype.__name__}: {value}") | |
with hide_traceback(): | |
1/0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment