exception thrown in pycharm debug mode but not in run mode (seaborn, matplotlib)

  • Last Update :
  • Techknowledgy :

When I run the following script, it is fine in run mode but in debug mode it throws an exception. Here's the script:

import numpy as np
import pandas as pd
import PySimpleGUI as sg
import seaborn as sns

from matplotlib.backends.backend_tkagg
import FigureCanvasTkAgg
from matplotlib.figure
import Figure

def init_data():
   r1 = np.random.rand(5, 4)
columns = [f "var{i}"
   for i in range(1, 5)
]
df = pd.DataFrame(r1, columns = columns)
df.insert(0, 'year', range(2021, 2026))
df.insert(1, 'scenario', 'test1')
ldf = pd.melt(df, id_vars = ['year', 'scenario'], value_vars = columns, var_name = 'percentile', value_name = 'carbon')
return ldf

def draw_figure(canvas, figure):
   figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
figure_canvas_agg.draw()
figure_canvas_agg.get_tk_widget().pack(side = 'top', fill = 'both', expand = 1)
return figure_canvas_agg

# define the window layout
layout = [
   [sg.Text('Plot test')],
   [sg.Canvas(key = '-CANVAS-')],
   [sg.Button('Ok')]
]

# create the form and show it without the plot
window = sg.Window('Testing seaborn in PySimpleGUI', layout, finalize = True,
   element_justification = 'center', font = 'Helvetica 18')

figure = Figure()
ax = figure.subplots()
sns.lineplot(x = 'year', y = 'carbon', hue = 'percentile', data = init_data(), ax = ax)

# add the plot to the window
fig_canvas_agg = draw_figure(window['-CANVAS-'].TKCanvas, figure)

event, values = window.read()

window.close()

Suggestion : 2

PyCharm 2021.2.3 Community Edition,When I run the following script, it is fine anycodings_python in run mode but in debug mode it throws an anycodings_python exception. Here's the script:,Aligning an image/carousel to the bottom of multiple without the use of positioning,How to fill or shade area between two corresponding points in stacked bar plots in Python using matplotlib?

When I run the following script, it is fine anycodings_python in run mode but in debug mode it throws an anycodings_python exception. Here's the script:

import numpy as np
import pandas as pd
import PySimpleGUI as sg
import seaborn as sns

from matplotlib.backends.backend_tkagg
import FigureCanvasTkAgg
from matplotlib.figure
import Figure

def init_data():
   r1 = np.random.rand(5, 4)
columns = [f "var{i}"
   for i in range(1, 5)
]
df = pd.DataFrame(r1, columns = columns)
df.insert(0, 'year', range(2021, 2026))
df.insert(1, 'scenario', 'test1')
ldf = pd.melt(df, id_vars = ['year', 'scenario'], value_vars = columns, var_name = 'percentile', value_name = 'carbon')
return ldf

def draw_figure(canvas, figure):
   figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
figure_canvas_agg.draw()
figure_canvas_agg.get_tk_widget().pack(side = 'top', fill = 'both', expand = 1)
return figure_canvas_agg

# define the window layout
layout = [
   [sg.Text('Plot test')],
   [sg.Canvas(key = '-CANVAS-')],
   [sg.Button('Ok')]
]

# create the form and show it without the plot
window = sg.Window('Testing seaborn in PySimpleGUI', layout, finalize = True,
   element_justification = 'center', font = 'Helvetica 18')

figure = Figure()
ax = figure.subplots()
sns.lineplot(x = 'year', y = 'carbon', hue = 'percentile', data = init_data(), ax = ax)

# add the plot to the window
fig_canvas_agg = draw_figure(window['-CANVAS-'].TKCanvas, figure)

event, values = window.read()

window.close()

Suggestion : 3

Exception thrown in PyCharm debug mode but not in run mode (seaborn, matplotlib),How to print python exception thrown inside exec() function from outside,Solution for checking if exception is thrown and not exiting the script,How to catch Python exception thrown in C++ code wrapped in a boost python module

Sorry for posting, I've got the answer. At first, I tried

except:
   print(sys.exc_info()[0])

Then I've got the <class 'GeneratorExit'>. So, I can finally catch the exception by

except GeneratorExit:
   print('caught you')