Why do I get a list of errors when trying to load

2020-07-09 07:24发布

问题:

I have installed PyCharm with Anaconda. I installed numpy fine using the PyCharm settings by adding the package via the Project Interpreter tab. However I am now trying to install matplotlib and I get a list of errors.

Just by including the line

import matplotlib.pyplot as plt

I get the errors:

AttributeError: module 'matplotlib.pyplot' has no attribute 'switch_backend'
Matplotlib support failed
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2018.2.4\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 23, in do_import
    succeeded = activate_func()
  File "C:\Program Files\JetBrains\PyCharm 2018.2.4\helpers\pydev\_pydev_bundle\pydev_console_utils.py", line 199, in <lambda>
    "matplotlib": lambda: activate_matplotlib(self.enableGui),
  File "C:\Program Files\JetBrains\PyCharm 2018.2.4\helpers\pydev\pydev_ipython\matplotlibtools.py", line 96, in activate_matplotlib
    gui, backend = find_gui_and_backend()
  File "C:\Program Files\JetBrains\PyCharm 2018.2.4\helpers\pydev\pydev_ipython\matplotlibtools.py", line 47, in find_gui_and_backend
    backend = matplotlib.rcParams['backend']
  File "C:\Users\calcl\Anaconda3\envs\PXP\lib\site-packages\matplotlib\__init__.py", line 892, in __getitem__
    plt.switch_backend(rcsetup._auto_backend_sentinel)

What could be causing this problem and how would I resolve it?

I am using Python 3.6 with 3.0.0 Matplotlib and PyCharm 2018.2.4

回答1:

I encountered the same error using Python 3.6 with 3.0.0 Matplotlib and PyCharm 2018.2.4. The error is evidently on the Pycharm side, as indicated by the traceback and by the fact that importing matplotlib via the anaconda prompt or spyder IDE does not produce this error.

The 'switch backend' utility seems to be a new feature according to the release notes of matplotlib https://github.com/matplotlib/matplotlib/releases. As pointed out in @Psychotechnopath's answer, it might be some issue with the path, so that Pycharm can't find the switch backend module when it is called by the Pycharm scripts in your Traceback.

However I don't think manually adding anything to the path is a desirable and robust solution. Instead, I downgraded matplotlib to version 2.2.3 using the conda installer in the Anaconda Prompt:

conda install matplotlib=2.2.3

After this downgrade I was able to import matplotlib in Pycharm again without any issues.



回答2:

Like @BigFish wrote, this is a bug in PyCharm side, you can see the bug report here.

It's fixed in PyCharm 2018.3, so the easiest solution is to upgrade PyCharm. Alternative workarounds are downgrading matplotlib, or unchecking "run with python console" in the run configuration:

This will allow you to run/debug, but you'll still get those errors if you use the interactive console.

As a side note, PyCharm has a history of lagging behind matplotlib API changes, so this should be your first guess next time...



回答3:

Probably you did not "add to path" when installing Anaconda, or you are not running PyCharm from an activated conda environment. Running Pycharm without an activated environment is unsupported, and results in issues when trying to install packages. Two solutions you could try:

  • Reinstall Anaconda and tick the option "Add to path". Make sure you know what this means, by for instance looking at this post: Why (or why not) Add Anaconda to path? because if you have multiple python installations on path this could cause issues (That's why Anaconda warns you to do this when installing). Summarized, adding Anaconda to path makes it easier for programs like PyCharm to find where everything is installed, thus decreasing the chance for import errors.

  • Boot Anaconda prompt, and activate the environment you wish to use, by activate Environmentname. If you never use seperate environments, but just the base/root environment you don't need to activate anything, since booting the Anaconda prompt automatically activates the base/root environment. After the environment of choice is activated, boot PyCharm from this prompt.

Let me know if that solved your issues =)