VS Code中Jupyter使用Panel遇ImportError问题求助
VS Code Jupyter环境中Panel与Bokeh版本兼容问题
问题描述
在VS Code的Jupyter环境中使用Panel时遭遇ImportError:
- 初始使用Bokeh 3.1.1时,无法从
bokeh.util.warnings导入warn; - 尝试降级到Bokeh 2.4.0、2.4.3版本后问题加剧,又出现无法从
bokeh.models导入Box的错误。
环境依赖
导入的库包括:
- pandas
- warnings
- numpy
- panel
- panel.extension('tabulator')
- hvplot.pandas
测试代码
data_slider = pn.widgets.DateRangeSlider( name='Periodo', start=df_base['DATA'].min(), end=df_base['DATA'].max(), value=(df_base['DATA'].min(), df_base['DATA'].max()), step=2 ) data_slider
首次报错信息
--------------------------------------------------------------------------- ImportError Traceback (most recent call last) File c:\Users\acunha\AppData\Local\Programs\Python\Python311\Lib\site-packages\IPython\core\formatters.py:974, in MimeBundleFormatter.__call__(self, obj, include, exclude) 971 method = get_real_method(obj, self.print_method) 973 if method is not None: ---> 974 return method(include=include, exclude=exclude) 975 return None 976 else: File c:\Users\acunha\AppData\Local\Programs\Python\Python311\Lib\site-packages\panel\viewable.py:623, in Viewable._repr_mimebundle_(self, include, exclude) 620 loaded = hv.extension._loaded 622 if config.comms in ('vscode', 'ipywidgets'): ---> 623 widget = ipywidget(self) 624 if hasattr(widget, '_repr_mimebundle_'): 625 return widget._repr_mimebundle_(include=include, exclude=exclude), {} File c:\Users\acunha\AppData\Local\Programs\Python\Python311\Lib\site-packages\panel\io\notebook.py:384, in ipywidget(obj, **kwargs) 367 def ipywidget(obj: Any, **kwargs: Any): 368 """ 369 Returns an ipywidget model which renders the Panel object. 370 (...) 382 Returns an ipywidget model which renders the Panel object. 383 """ ---> 384 from jupyter_bokeh.widgets import BokehModel 386 from ..pane import panel 387 model = panel(obj, **kwargs).get_root() File c:\Users\acunha\AppData\Local\Programs\Python\Python311\Lib\site-packages\jupyter_bokeh\__init__.py:9 5 from ._version import __version__ 7 HERE = Path(__file__).parent.resolve() ----> 9 from .widgets import BokehModel 10 with (HERE / "labextension" / "package.json").open() as fid: 11 data = json.load(fid) File c:\Users\acunha\AppData\Local\Programs\Python\Python311\Lib\site-packages\jupyter_bokeh\widgets.py:33 31 from bokeh.models import ColumnDataSource, LayoutDOM 32 from bokeh.protocol import Protocol ---> 33 from bokeh.core.serialization import Deserializer, Serialized 34 from bokeh.model import Model 36 from ._version import __version__ File c:\Users\acunha\AppData\Local\Programs\Python\Python311\Lib\site-packages\bokeh\core\serialization.py:63 47 from ..util.dataclasses import ( 48 Unspecified, 49 dataclass, 50 entries, 51 is_dataclass, 52 ) 53 from ..util.serialization import ( 54 array_encoding_disabled, 55 convert_datetime_type, (...) 61 transform_series, 62 ) ---> 63 from ..util.warnings import BokehUserWarning, warn 64 from .types import ID 66 if TYPE_CHECKING: ImportError: cannot import name 'warn' from 'bokeh.util.warnings' (c:\Users\acunha\AppData\Local\Programs\Python\Python311\Lib\site-packages\bokeh\util\warnings.py)
后续报错信息
--------------------------------------------------------------------------- ImportError Traceback (most recent call last) Cell In[1], line 4 2 import warnings 3 import numpy as np ----> 4 import panel as pn 5 pn.extension('tabulator') 6 import hvplot.pandas File c:\Users\acunha\AppData\Local\Programs\Python\Python311\Lib\site-packages\panel\__init__.py:48 1 """ 2 Panel is a high level app and dashboarding framework 3 ==================================================== (...) 46 https://panel.holoviz.org/getting_started/index.html 47 """ ---> 48 from . import layout # noqa 49 from . import links # noqa 50 from . import pane # noqa File c:\Users\acunha\AppData\Local\Programs\Python\Python311\Lib\site-packages\panel\layout\__init__.py:31 1 """ 2 Layout 3 ====== (...) 29 https://panel.holoviz.org/getting_started/index.html 30 """ ---> 31 from .accordion import Accordion # noqa 32 from .base import ( # noqa 33 Column, ListLike, ListPanel, Panel, Row, WidgetBox, 34 ) 35 from .card import Card # noqa File c:\Users\acunha\AppData\Local\Programs\Python\Python311\Lib\site-packages\panel\layout\accordion.py:9 5 import param 7 from bokeh.models import Column as BkColumn, CustomJS ----> 9 from .base import NamedListPanel 10 from .card import Card 12 if TYPE_CHECKING: File c:\Users\acunha\AppData\Local\Programs\Python\Python311\Lib\site-packages\panel\layout\base.py:17 13 import param 15 from bokeh.models import Column as BkColumn, Row as BkRow ---> 17 from ..io.model import hold 18 from ..io.state import state 19 from ..reactive import Reactive File c:\Users\acunha\AppData\Local\Programs\Python\Python311\Lib\site-packages\panel\io\__init__.py:9 7 from .cache import cache # noqa 8 from .callbacks import PeriodicCallback # noqa ----> 9 from .document import init_doc, unlocked, with_lock # noqa 10 from .embed import embed_state # noqa 11 from .logging import panel_logger # noqa File c:\Users\acunha\AppData\Local\Programs\Python\Python311\Lib\site-packages\panel\io\document.py:20 17 from bokeh.document.document import Document 18 from bokeh.document.events import DocumentChangedEvent, ModelChangedEvent ---> 20 from .model import monkeypatch_events 21 from .state import curdoc_locked, state 23 logger = logging.getLogger(__name__) File c:\Users\acunha\AppData\Local\Programs\Python\Python311\Lib\site-packages\panel\io\model.py:20 16 from bokeh.document.events import ( 17 ColumnDataChangedEvent, DocumentPatchedEvent, ModelChangedEvent, 18 ) 19 from bokeh.model import DataModel ---> 20 from bokeh.models import Box, ColumnDataSource, Model 21 from bokeh.protocol import Protocol 22 from typing_extensions import Literal ImportError: cannot import name 'Box' from 'bokeh.models' (c:\Users\acunha\AppData\Local\Programs\Python\Python311\Lib\site-packages\bokeh\models\__init__.py)
解决方法
问题根源
这两个错误都是Panel与Bokeh版本不匹配导致的:
- Bokeh 3.x移除了
bokeh.util.warnings.warn接口,但当前使用的Panel版本依赖该旧接口; - 降级到Bokeh 2.4.x后,Panel版本又依赖Bokeh 3.x才新增的
Box类,因此出现新的导入错误。
具体步骤
- 彻底卸载现有依赖:
pip uninstall -y panel bokeh jupyter_bokeh - 安装兼容版本组合:
- 若想使用较新的Panel版本,推荐安装Panel 1.x + Bokeh 3.2.x(稳定兼容):
pip install panel bokeh==3.2.2 jupyter_bokeh - 若需要更稳定的旧版本组合,可选择Panel 0.14.x + Bokeh 2.4.3:
pip install panel==0.14.4 bokeh==2.4.3 jupyter_bokeh
- 若想使用较新的Panel版本,推荐安装Panel 1.x + Bokeh 3.2.x(稳定兼容):
- 重启环境:关闭VS Code后重新打开,重启Jupyter内核,再运行代码即可。
内容的提问来源于stack exchange,提问作者André Cunha
相关产品推荐
相关产品推荐

