简体   繁体   English

PyCharm 导入 matplotlib.pyplot 显示错误

[英]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进程以退出代码 1 结束

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

Python: 2.7.16蟒蛇:2.7.16

PyCharm Professional: 2019.2 PyCharm 专业版: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简单的答案:禁用“在科学窗口中显示绘图”(设置 -> 工具 -> Python Scientific)或降级 PyCharm 或将您的项目移动到 python3
Remember to add plt.show() in your code.记得在你的代码中添加plt.show()

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/您需要编写自己的导入钩子来找到urllib.parseurllib.request (要求display_.py文件中的下一行。更多您可以在这里阅读https://xion.org.pl/2012/05/06/hacking -蟒蛇进口/

(i'm not enough familiar with python 2 import system to write it) (我对python 2导入系统不够熟悉,无法编写它)

For python 2 use对于 python 2 使用

from urlparse import urlparse

If you need to write code which is Python2 and Python3 compatible you can use the following import如果您需要编写与 Python2 和 Python3 兼容的代码,您可以使用以下导入

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

In your PyCharm project:在您的 PyCharm 项目中:

  • press Ctrl+Alt+s to open the settings按 Ctrl+Alt+s 打开设置
  • on the left column, select Project Interpreter在左列中,选择 Project Interpreter
  • on the top right there is a list of python binaries found on your system, pick the right one在右上角有一个在您的系统上找到的 python 二进制文件列表,选择正确的
  • eventually click the + button to install additional python modules, in your case, it is parse module is missing so install that one最终单击 + 按钮安装其他 python 模块,在您的情况下,缺少解析模块,因此安装该模块

As mentioned by @Grzegorz Bokota, the problem is coming from the "scientific view mode" of PyCharm.正如@Grzegorz Bokota 所提到的,问题来自 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.这种模式允许可视化图形,因此调用 matplotlib,如果您使用的是 Python 2,它可能是一个不兼容的版本。 这个错误已经在这里发现,看来我们只需要等待下一个版本来解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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