Skip to content

Instantly share code, notes, and snippets.

@waszil
Created July 8, 2020 07:46
Show Gist options
  • Save waszil/fccda8749ed5dd5baa336bae8e88dd7a to your computer and use it in GitHub Desktop.
Save waszil/fccda8749ed5dd5baa336bae8e88dd7a to your computer and use it in GitHub Desktop.
Disable traceback printing temporarily in python
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