pydev无法解析的导入,但是代码在空闲状态下运行

时间:2019-02-08 22:16:21

标签: python-3.6 pydev

我正在尝试为我正在编写的绘图程序编写一个简单的GUI,它在Idle中运行(Python 3.6.4(v3.6.4:d48eceb,2017年12月19日,06:04:45)[MSC v。 1900 Win 32上的32位元(Intel)],没有问题,但是当我尝试在Eclipse中运行pydev时,脚本不起作用。

我在eclipse中打开控制台,并尝试导入FigureCanvas(唯一的错误eclipse显示给我),我得到的是我得到的错误消息:

from matplotlib.backends.backend_qt5agg import FigureCanvas
Backend TkAgg is interactive backend. Turning interactive mode on.
Traceback (most recent call last):
  File "C:\Users\Daniel\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\backends\qt_compat.py", line 198, in <module>
    from PySide import QtCore, QtGui, __version__, __version_info__
  File "C:\Users\Daniel\.p2\pool\plugins\org.python.pydev.core_7.1.0.201902031515\pysrc\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'PySide'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\Daniel\.p2\pool\plugins\org.python.pydev.core_7.1.0.201902031515\pysrc\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\Daniel\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 16, in <module>
    from .backend_qt5 import (
  File "C:\Users\Daniel\.p2\pool\plugins\org.python.pydev.core_7.1.0.201902031515\pysrc\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\Daniel\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\backends\backend_qt5.py", line 18, in <module>
    import matplotlib.backends.qt_editor.figureoptions as figureoptions
  File "C:\Users\Daniel\.p2\pool\plugins\org.python.pydev.core_7.1.0.201902031515\pysrc\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\Daniel\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\backends\qt_editor\figureoptions.py", line 20, in <module>
    import matplotlib.backends.qt_editor.formlayout as formlayout
  File "C:\Users\Daniel\.p2\pool\plugins\org.python.pydev.core_7.1.0.201902031515\pysrc\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\Daniel\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\backends\qt_editor\formlayout.py", line 56, in <module>
    from matplotlib.backends.qt_compat import QtGui, QtWidgets, QtCore
  File "C:\Users\Daniel\.p2\pool\plugins\org.python.pydev.core_7.1.0.201902031515\pysrc\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\Daniel\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\backends\qt_compat.py", line 201, in <module>
    "Matplotlib qt-based backends require an external PyQt4, PyQt5,\n"
ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5,
PySide or PySide2 package to be installed, but it was not found.

这是在空闲(有效)和eclipse(无效)上运行的代码的一部分     #mplwidget.py     从PyQt5.QtWidgets导入*

from matplotlib.backends.backend_qt5agg import FigureCanvas

from matplotlib.figure import Figure


class MplWidget(QWidget):

    def __init__(self, parent = None):

        QWidget.__init__(self, parent)

        self.canvas = FigureCanvas(Figure())

        vertical_layout = QVBoxLayout()
        vertical_layout.addWidget(self.canvas)

        self.canvas.axes = self.canvas.figure.add_subplot(111)
        self.setLayout(vertical_layout)

这是调用mpwidget的主要功能

# ------------------------------------------------------
# ---------------------- main.py -----------------------
# ------------------------------------------------------
from PyQt5.QtWidgets import*
from PyQt5.uic import loadUi

from matplotlib.backends.backend_qt5agg import (NavigationToolbar2QT as NavigationToolbar)

import numpy as np
import random

class MatplotlibWidget(QMainWindow):

    def __init__(self):

        QMainWindow.__init__(self)

        loadUi("UI.ui",self)

        self.setWindowTitle("PyQt5 & Matplotlib Example GUI")

        #self.pushButton_generate_random_signal.clicked.connect(self.update_graph)

        self.addToolBar(NavigationToolbar(self.MplWidget.canvas, self))


    def update_graph(self):

        fs = 500
        f = random.randint(1, 100)
        ts = 1/fs
        length_of_signal = 100
        t = np.linspace(0,1,length_of_signal)

        cosinus_signal = np.cos(2*np.pi*f*t)
        sinus_signal = np.sin(2*np.pi*f*t)

        self.MplWidget.canvas.axes.clear()
        self.MplWidget.canvas.axes.plot(t, cosinus_signal)
        self.MplWidget.canvas.axes.plot(t, sinus_signal)
        self.MplWidget.canvas.axes.legend(('cosinus', 'sinus'),loc='upper right')
        self.MplWidget.canvas.axes.set_title('Cosinus - Sinus Signal')
        self.MplWidget.canvas.draw()


app = QApplication([])
window = MatplotlibWidget()
window.show()
app.exec_()

当我尝试在eclipse中运行代码时,什么也没有发生,但是当它在空闲状态下运行时,一切都会打开我期望的方式

1 个答案:

答案 0 :(得分:1)

这通常意味着某些环境变量在两种环境中的配置方式都不相同。

请查看常见问题解答:http://www.pydev.org/faq.html#MyProgramDoesNotWorkInPyDev,以了解如何进行操作(主要是在这两者上同时打印环境/ PYTHONPATH,以检查差异并修复PyDev上的配置)。