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()回溯(最近一次调用):
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进程已完成,退出代码为%1
=================
Python: 2.7.16
PyCharm专业: 2019.2
=================
顺便说一下,在控制台模式下运行的代码是工作的。
发布于 2019-07-30 09:09:57
简单的答案:禁用“显示科学窗口中的绘图”(设置-> Tools -> python3 )或将PyCharm降级或将项目移动到python3
记住在代码中添加plt.show()。
更复杂一点。您需要编写自己的导入钩子才能找到urllib.parse和urllib.request ( display_.py文件中的下一行)。更多您可以在这里阅读https://xion.org.pl/2012/05/06/hacking-python-imports/
(我不太熟悉python 2导入系统,无法编写它)
发布于 2019-07-30 08:16:07
用于python2使用
from urlparse import urlparse如果需要编写与Python2和Python3兼容的代码,可以使用以下导入
try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse在您的PyCharm项目中:
发布于 2019-08-05 08:03:27
正如@Grzegorz所提到的,问题来自于PyCharm的“科学视角模式”。这种模式允许可视化图形,因此调用matplotlib,如果您使用Python2,它可能是一个不兼容的版本。这个bug已经被识别为这里,似乎我们只需要等待下一个版本才能解决它。
https://stackoverflow.com/questions/57267016
复制相似问题