You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jupyter Notebook导入Pandas报TypeError错误求助(Python3.8)

问题

Python 3.8版本环境中,在Jupyter Notebook执行import pandas as pd时触发TypeError错误,但在VS Code中导入Pandas完全正常。已卸载并重装Jupyter Notebook和Pandas,问题仍未解决,具体错误堆栈如下:

TypeError                                 Traceback (most recent call last) Input In [1], in <cell line: 1>()
----> 1 import pandas as pd File c:\users\lenovo\appdata\local\programs\python\python38\lib\site-packages\pandas\__init__.py:22, in <module>
     19 del hard_dependencies, dependency, missing_dependencies
     21 # numpy compat
---> 22 from pandas.compat import is_numpy_dev as _is_numpy_dev
     24 try:
     25     from pandas._libs import hashtable as _hashtable, lib as _lib, tslib as _tslib File c:\users\lenovo\appdata\local\programs\python\python38\lib\site-packages\pandas\compat\__init__.py:20, in <module>
     14 from pandas._typing import F
     15 from pandas.compat.numpy import (
     16     is_numpy_dev,
     17     np_version_under1p19,
     18     np_version_under1p20,
     19 )
---> 20 from pandas.compat.pyarrow import (
     21     pa_version_under1p01,
     22     pa_version_under2p0,
     23     pa_version_under3p0,
     24     pa_version_under4p0,
     25 )
     27 PY39 = sys.version_info >= (3, 9)
     28 PY310 = sys.version_info >= (3, 10) File c:\users\lenovo\appdata\local\programs\python\python38\lib\site-packages\pandas\compat\pyarrow.py:6, in <module>
      3 from pandas.util.version import Version
      5 try:
---> 6     import pyarrow as pa
      8     _pa_version = pa.__version__
      9     _palv = Version(_pa_version) File c:\users\lenovo\appdata\local\programs\python\python38\lib\site-packages\pyarrow\__init__.py:65, in <module>
     63 _gc_enabled = _gc.isenabled()
     64 _gc.disable()
---> 65 import pyarrow.lib as _lib
     66 if _gc_enabled:
     67     _gc.enable() File c:\users\lenovo\appdata\local\programs\python\python38\lib\site-packages\pyarrow\compat.pxi:43, in init pyarrow.lib() File c:\users\lenovo\appdata\local\programs\python\python38\lib\site-packages\cloudpickle\__init__.py:3, in <module>
      1 from __future__ import absolute_import
----> 3 from cloudpickle.cloudpickle import *
      5 __version__ = '0.6.1' File c:\users\lenovo\appdata\local\programs\python\python38\lib\site-packages\cloudpickle\cloudpickle.py:167, in <module>
    147     else:
    148         return types.CodeType(
    149             co.co_argcount,
    150             co.co_kwonlyargcount,    (...)
    163             (),
    164         )
--> 167 _cell_set_template_code = _make_cell_set_template_code()
    170 def cell_set(cell, value):
    171     """Set the value of a closure cell.
    172     """ File c:\users\lenovo\appdata\local\programs\python\python38\lib\site-packages\cloudpickle\cloudpickle.py:148, in _make_cell_set_template_code()
    131     return types.CodeType(
    132         co.co_argcount,
    133         co.co_nlocals,    (...)
    145         (),
    146     )
    147 else:
--> 148     return types.CodeType(
    149         co.co_argcount,
    150         co.co_kwonlyargcount,
    151         co.co_nlocals,
    152         co.co_stacksize,
    153         co.co_flags,
    154         co.co_code,
    155         co.co_consts,
    156         co.co_names,
    157         co.co_varnames,
    158         co.co_filename,
    159         co.co_name,
    160         co.co_firstlineno,
    161         co.co_lnotab,
    162         co.co_cellvars,  # this is the trickery
    163         (),
    164     ) TypeError: an integer is required (got type bytes)
解决方案

从错误堆栈可定位问题根源:cloudpickle 0.6.1版本不兼容Python 3.8的types.CodeType参数规范,导致初始化失败,进而阻断了pandas的导入流程。可按以下步骤解决:

  • 升级cloudpickle至兼容Python 3.8的版本,执行命令:
    pip install --upgrade cloudpickle
    
  • 若升级后问题仍存在,先卸载旧版本再重装:
    pip uninstall -y cloudpickle
    pip install cloudpickle
    
  • 验证Jupyter Notebook使用的Python环境是否与VS Code一致:在Jupyter单元格中执行!where python(Windows)或!which python(Linux/macOS),查看路径是否匹配。若不一致,需切换Jupyter内核到正确的Python环境。

内容的提问来源于stack exchange,提问作者Mohammed Umar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 23:55:22