8

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

Cameron
  • 1,011
  • 1
  • 10
  • 20
  • The offending line is [this](https://github.com/matplotlib/matplotlib/blob/0881b9c94f50cf2e81280d61e28bdf252c0f463d/lib/matplotlib/__init__.py#L892). It sounds strange that pyplot is claimed not to have a `switch_backend` because it [obviously does](https://github.com/matplotlib/matplotlib/blob/0881b9c94f50cf2e81280d61e28bdf252c0f463d/lib/matplotlib/pyplot.py#L177). I would hence think that either the install is broken or incomplete, or PyCharm shaddows some matplotlib functions. – ImportanceOfBeingErnest Oct 17 '18 at 17:40
  • It also sounds similar to [this report](https://github.com/matplotlib/matplotlib/issues/12362#issuecomment-427704873), which has unfortunately never been clarified. – ImportanceOfBeingErnest Oct 17 '18 at 17:47

3 Answers3

11

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:

run with console

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...

OmerB
  • 4,134
  • 3
  • 20
  • 33
9

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.

BigFish
  • 106
  • 3
1

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 =)

Psychotechnopath
  • 2,471
  • 5
  • 26
  • 47
  • Since the error is triggered from a file within the conda env ("PXP" in this case), I don't quite see how that would solve the issue. Is there a concrete explanation for this happening if anaconda is not "added to path"? – ImportanceOfBeingErnest Oct 17 '18 at 18:47
  • @ImportanceOfBeingErnest I experienced similar import issues when trying to import Numpy and matplotlib, and went as far as sending a bug-report (https://github.com/conda/conda/issues/7833) to Continuum about this. A developer told me that import-related issues can arise when you are running a conda environment which is not activated. I think the problem is, when you do not add Anaconda to path during installation, Pycharm can not activate your conda environment, thus resulting in import errors. After reinstalling and adding to path every import worked fine for me. – Psychotechnopath Oct 17 '18 at 18:55
  • @ImportanceOfBeingErnest If you are interested please take the time to read the bug-report, as it gives a lot more in-depth info. Small quote that can be seen as "concrete explanation" imo (Developer said this): "To run any software from The Anaconda Distribution you must either activate that env or add the values to PATH (i.e. let the installer do this) - THIS IS NOT RECOMMENDED." – Psychotechnopath Oct 17 '18 at 19:00
  • Yep sure. So my point is that because the error message shows the activated environment (I suppose here that "PXP" is not the (base).), that PyCharm is correctly launched in that environment. If that should not be the case, the answer is of course useful. – ImportanceOfBeingErnest Oct 17 '18 at 19:06
  • I understand. Speaking from my own experience: In my case PyCharm also showed the correct path to the environment, but still all modules failed to import. We will see, let's hope it is useful =) – Psychotechnopath Oct 17 '18 at 19:34