0

I am developing a Python package for use in Jupyter Notebooks. It will also be accessed via python module scripts but the Jupyter Notebook is an interface I am targetting. However the handling of errors is very ugly within Jupyter notebooks as there is no simple way to set up a try-catch mechanism within each cell.

I am therefore thinking of replacing all of my exception-raising with a simple print statement for Jupyter notebooks but with standard exceptions for non-Jupyter environments such as Spyder. To do this I will need to set some global variable within the Python package, which is do-able. So the user simply gets a polite error rather than a listing with a Python error report showing the culprit line.

Does anyone have any suggestions for a better approach ?

Dom
  • 160
  • 11

1 Answers1

1

May be have a look at the warning package.

import warnings

warnings.warn("deprecated", DeprecationWarning)

It also supports other warning types like Runtimewarning , Futurewarning etc..

https://docs.python.org/3/library/warnings.html

Edit You can have exception hook and limit the traceback and instead print warnings using warnings package.

Refer to this StackOverflow post Hide traceback unless a debug flag is set

abhilb
  • 5,639
  • 2
  • 20
  • 26
  • OK. But I still can't trap exceptions if I do that can I ? I can just reduce the number of error messages ? – Dom Nov 23 '19 at 19:20