简体   繁体   中英

PyCharm import matplotlib.pyplot show error

import numpy as np
import matplotlib.pyplot as plt

def main():
    x = np.arange(0, 5, 0.1)
    y = np.sin(x)
    plt.plot(x, y)

if __name__ == '__main__':
    main()

Traceback (most recent call last):

  File 
"/Users/tim/workspace/Python/MachineLearn/test.py", line 2, in <module>
    import matplotlib.pyplot as plt
  File "/usr/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 115, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 63, in pylab_setup
    [backend_name], 0)
  File "/Applications/PyCharm.app/Contents/helpers/pycharm_matplotlib_backend/backend_interagg.py", line 11, in <module>
    from datalore.display import display
  File "/Applications/PyCharm.app/Contents/helpers/pycharm_display/datalore/display/__init__.py", line 1, in <module>
    from .display_ import *
  File "/Applications/PyCharm.app/Contents/helpers/pycharm_display/datalore/display/display_.py", line 5, in <module>
    from urllib.parse import urlencode
ImportError: No module named parse

Process finished with exit code 1

=================

Python: 2.7.16

PyCharm Professional: 2019.2

=================

btw, the code run in console mode is work

Simple answer: disable "show plots in scientific window" (Settings -> Tools -> Python Scientific) or downgrade the PyCharm or move your project to python3
Remember to add plt.show() in your code.

A little more complicated. You need to write own importing hooks to find that urllib.parse and urllib.request (next line in display_.py file are requested. More you can read here https://xion.org.pl/2012/05/06/hacking-python-imports/

(i'm not enough familiar with python 2 import system to write it)

For python 2 use

from urlparse import urlparse

If you need to write code which is Python2 and Python3 compatible you can use the following import

try:
    from urllib.parse import urlparse
except ImportError:
     from urlparse import urlparse

In your PyCharm project:

  • press Ctrl+Alt+s to open the settings
  • on the left column, select Project Interpreter
  • on the top right there is a list of python binaries found on your system, pick the right one
  • eventually click the + button to install additional python modules, in your case, it is parse module is missing so install that one

As mentioned by @Grzegorz Bokota, the problem is coming from the "scientific view mode" of PyCharm. This mode allows to visualise graphs and is thus calling matplotlib, and probably an incompatible version of it if you are using Python 2. This bug has been identified here and it seems that we just have to wait for the next release to get it solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM