user169808
user169808

Reputation: 533

pydev unresolved import, but code runs on Idle

I'm trying to write a simple GUI for a graphing program I am writing, it runs in Idle(Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32) with no problems, but when I try to run in Eclipse, pydev, the script doesn't work.

I opened the console in eclipse and tried to import FigureCanvas(the only error eclipse is showing me) and I got this is the error message I get:

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.

this is the part of the code that is being run, both on idle(which works) and on eclipse(doesn't work) # mplwidget.py from PyQt5.QtWidgets import*

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)

this is the main function that calles the 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_()

when I try to run the code in eclipse nothing happens, but whe it runs on idle every thing opens the way I expect

Upvotes: 0

Views: 718

Answers (1)

Fabio Zadrozny
Fabio Zadrozny

Reputation: 25372

This usually means that some environment variable is not configured the same way in both environments.

Please take a look at the FAQ: http://www.pydev.org/faq.html#MyProgramDoesNotWorkInPyDev to see how to proceed (mainly print the environment/PYTHONPATH on both to check the differences and fix the config on PyDev).

Upvotes: 1

Related Questions