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

Matplotlib3.4.3导入报TypeError: expected str...not WindowsPath 求无降级方案

你遇到的TypeError: expected str, bytes or os.PathLike object, not WindowsPath报错,根源是matplotlib 3.4.3在Windows环境下读取配置文件时,拿到的路径是pathlib.WindowsPath类型,对应版本的os.path.expanduser接口无法直接处理该类型对象导致导入失败。

解决方案(无需降级matplotlib)

方案1:重置matplotlib配置文件

  • 运行命令获取配置文件目录:
    python -c "import matplotlib; print(matplotlib.get_configdir())"
  • 打开返回的目录,将里面的matplotlibrc文件重命名为matplotlibrc.bak备份,或者直接删除。重启Python进程重新导入matplotlib,软件会自动生成默认配置文件,解决配置异常导致的路径类型错误。

方案2:重装当前版本matplotlib

如果是安装包文件损坏导致的异常,直接卸载重装对应版本即可,不需要降级:

  • 卸载现有matplotlib:
    pip uninstall -y matplotlib
  • 清除pip本地缓存避免复用损坏的安装包:
    pip cache purge
  • 重新安装3.4.3版本(pip会直接拉取Windows预编译的wheel包,不会出现低版本需要编译缺依赖的问题):
    pip install matplotlib==3.4.3

方案3:临时修复(适合不想改环境的场景)

在你的Python代码导入matplotlib之前,添加代码手动指定配置文件路径为字符串类型:

import os
from pathlib import Path
os.environ["MATPLOTLIBRC"] = str(Path.home() / ".matplotlib" / "matplotlibrc")
import matplotlib

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 14:00:00