【问题标题】:AttributeError: module 'matplotlib' has no attribute 'get_data_path' on Visual Studio's jupyter-notebookAttributeError:模块“matplotlib”在 Visual Studio 的 jupyter-notebook 上没有属性“get_data_path”
【发布时间】:2020-12-28 18:59:40
【问题描述】:

当我想在 Visual Studio 的 jupyter-notebook 上导入 matplotlib.pyplot 时出现以下错误。

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
 in 
----> 1 import matplotlib.pyplot as plt

~/miniconda3/envs/firstSteps/lib/python3.8/site-packages/matplotlib/__init__.py in 
    903 # triggering resolution of _auto_backend_sentinel.
    904 rcParamsDefault = _rc_params_in_file(
--> 905     cbook._get_data_path("matplotlibrc"),
    906     # Strip leading comment.
    907     transform=lambda line: line[1:] if line.startswith("#") else line,

~/.local/lib/python3.8/site-packages/matplotlib/cbook/__init__.py in _get_data_path(*args)

AttributeError: module 'matplotlib' has no attribute 'get_data_path'

但如果我在导航器的 jupyter-notebook 上尝试相同的代码,则不会出现此错误。 所以我不明白为什么会出现这个错误,因为两个笔记本都在同一个内核下运行,并且安装了 matplotlib 3.3.1 版本。

如果有人能给我任何启示,我将不胜感激。 :)

【问题讨论】:

    标签: python matplotlib visual-studio-code jupyter-notebook kernel


    【解决方案1】:

    我也遇到了同样的问题,用下面的顺序解决了

    • 使用终端指向项目根文件夹
    • 假设您正在运行conda 环境。所以,通过
      conda activate env-name 激活环境
    • 运行 python 解释器:python 命令并检查天气 matplotib 已由\
      import matplotlib 安装
    • 运行code .命令打开该目录下的VScode。
    • 打开终端并检查天气环境已激活
    • 还检查天气 jupyter Ipython 内核在同一环境中运行
    • 如果任何环境不是那个,则将其更改为特定环境\

    这通常不会在pip 环境中发生,因为 VScode 不会激活两个 通道 [pip, conda] 或conda 环境在大多数情况下会自动运行。

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,因为我使用了错误的 python 内核。我在使用标准 python 内核时遇到了这个问题,但在使用虚拟环境(这是我通常使用的)时没有。

      【讨论】:

      • Welcome to Stack Overflow!这是作为答案发布的,但它并不试图回答这个问题。应该是编辑或评论。
      • 我知道,但我没有评论的声誉,它可能会在我发现这个问题时提供一些见解,因为我遇到了同样的问题并以这种方式解决了。
      • 如果你能解释得更多,可能会更有帮助
      • 我在使用内核时遇到了这个错误,该内核不是最新的(但几个月/一年前我已经使用了 matplotlib)。当我使用它是最新的时,问题就消失了。 (这不是一个实际的解决方案,但它为我解决了这个问题,并且可能是对其他人的提示)。
      猜你喜欢
      • 2018-04-29
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      • 2020-08-27
      • 2019-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多