PyCharm调试模式下seaborn、matplotlib代码报错但运行模式正常
问题描述
运行环境
- PyCharm 2021.2.3 社区版
- Python 解释器 3.10.0
- matplotlib 3.5.0
- seaborn 0.11.2
- numpy 1.21.4
- pandas 1.3.4
- PySimpleGUI 4.55.1
问题现象
以下脚本在普通运行模式下执行正常,但调试模式下会抛出异常:
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()
报错信息
Traceback (most recent call last): File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\core\getlimits.py", line 384, in __new__ dtype = numeric.dtype(dtype) TypeError: 'NoneType' object is not callable During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Program Files\JetBrains\PyCharm Community Edition 2021.2.3\plugins\python-ce\helpers\pydev\pydevd.py", line 1483, in _exec pydev_imports.execfile(file, globals, locals) # execute the script File "C:\Program Files\JetBrains\PyCharm Community Edition 2021.2.3\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "D:/OneDrive/OneDrive - Louise Pryor & Co Ltd/Actuarial/Carbon/Carbon/seaborntest.py", line 39, in <module> sns.lineplot(x='year', y='carbon', hue='percentile', data=init_data(), ax=ax) File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\seaborn\_decorators.py", line 46, in inner_f return f(**kwargs) File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\seaborn\relational.py", line 710, in lineplot p.plot(ax, kwargs) File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\seaborn\relational.py", line 557, in plot self._add_axis_labels(ax) File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\seaborn\_core.py", line 1194, in _add_axis_labels x_visible = any(t.get_visible() for t in ax.get_xticklabels()) File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axes\_base.py", line 75, in wrapper return get_method(self)(*args, **kwargs) File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axis.py", line 1249, in get_ticklabels return self.get_majorticklabels() File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axis.py", line 1201, in get_majorticklabels ticks = self.get_major_ticks() File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axis.py", line 1371, in get_major_ticks numticks = len(self.get_majorticklocs()) File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axis.py", line 1277, in get_majorticklocs return self.major.locator() File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\ticker.py", line 2113, in __call__ vmin, vmax = self.axis.get_view_interval() File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axis.py", line 1987, in getter return getattr(getattr(self.axes, lim_name), attr_name) File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axes\_base.py", line 781, in viewLim self._unstale_viewLim() File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axes\_base.py", line 776, in _unstale_viewLim self.autoscale_view(**{f"scale{name}": scale File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axes\_base.py", line 2932, in autoscale_view handle_single_axis( File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\axes\_base.py", line 2895, in handle_single_axis x0, x1 = locator.nonsingular(x0, x1) File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\ticker.py", line 1654, in nonsingular return mtransforms.nonsingular(v0, v1, expander=.05) File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\matplotlib\transforms.py", line 2880, in nonsingular if maxabsvalue < (1e6 / tiny) * np.finfo(float).tiny: File "C:\Users\drlou\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\core\getlimits.py", line 387, in __new__ dtype = numeric.dtype(type(dtype)) TypeError: 'NoneType' object is not callable
解决方案
该问题是PyCharm 2021版本调试器与Python 3.10、对应版本numpy的兼容冲突导致的,调试器会意外将numpy.numeric.dtype覆盖为None,触发调用异常。可按以下优先级尝试解决:
- 关闭PyCharm调试器的Gevent兼容选项:打开路径
File > Settings > Build, Execution, Deployment > Python Debugger,取消勾选Gevent compatible选项,重启调试即可生效,这是该问题最常用的解决方式。 - 升级PyCharm版本:PyCharm 2022.1及以上版本已经修复了Python 3.10的调试兼容问题,升级到最新社区版即可彻底解决冲突。
- 降级Python版本到3.9.x:如果暂时无法升级PyCharm,将Python解释器替换为3.9版本,搭配适配的numpy 1.21.x版本也可以规避该问题。
- 临时补丁:如果以上方案都无法执行,可在脚本开头导入numpy后手动重置dtype引用:
import numpy as np np.numeric.dtype = np.dtype
该操作会强制修正被调试器覆盖的dtype引用,保证后续代码调用正常。
内容的提问来源于stack exchange,提问作者lpryor
相关产品推荐
相关产品推荐

