0

enter image description here

I'm using Canopy version 1.6.4 (latest version as of April 2016) and I simply wish to make a dialogue to ask the user browse to a Folder (directory) in a pop-up window, and for python to take the folder name and path as a variable.

Whatever I try when using PyQt4, I keep getting the following error message:

ImportError: Importing PyQt4 disabled by IPython, which has already imported an Incompatible QT Binding: pyside

I've found lots of other people experiencing a similar thing (e.g.: How can I use Pyqt with Enthought Canopy and this answer which I found incomprehensible : https://github.com/ipython/ipython/issues/2955/ ), but no simple answer as to how to solve this (I'm pretty new to the Python and Python environments). Can anyone recommend a quick fox, or better still another way of making a simple x-platform (Mac and windows) Dialogue box (Tkinter doesn't work either on Canopy!!!).

This enclosed screenshot isn't for a browser window, but this gives the same error message; as does Jupiter notebook and iPython.

FYI: Even without importing PySide I get this error! (I imported it once only, but not since).

Thanks!

Community
  • 1
  • 1
thescoop
  • 504
  • 5
  • 20

1 Answers1

1

There are two different python bindings for Qt -- PyQt and PySide. You cannot use both at the same time. You cannot even import both of them into the same python session. I'm guessing you are launching this from within an embedded python console inside your IDE, which has chosen to use PySide (which is why you're getting this error).

You have two options.

  1. Use PySide instead of PyQt.
  2. Launch your script from a regular console (eg. cmd.exe, terminal, bash) instead of the embedded console inside your IDE (PySide won't be pre-loaded and you can use PyQt).

PyQt and PySide are very similar, and in most cases you can just change the import statements.

from PySide import QtGui, QtCore

For your original question of how to create a dialog to pick a directory, you can use QFileDialog.getExistingDirectory

import sys
from PySide import QtGui, QtCore


class Dialog(QtGui.QDialog):

    def __init__(self, parent):
        super(Dialog, self).__init__(parent)
        self.ui_lay = QtGui.QHBoxLayout()
        self.setLayout(self.ui_lay)
        self.ui_line = QtGui.QLineEdit(self)
        self.ui_lay.addWidget(self.ui_line)
        self.ui_btn = QtGui.QPushButton('...', self)
        self.ui_lay.addWidget(self.ui_btn)
        self.ui_btn.clicked.connect(self.browse)

    @QtCore.Slot()  # for pyqt, QtCore.pyqtSlot()
    def browse(self):
        path = QtGui.QFileDialog.getExistingDirectory(self, 'Pick a Folder')
        if path:
            self.ui_line.setText(path)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    dlg = Dialog(None)
    dlg.show()
    app.exec_()
Brendan Abel
  • 35,343
  • 14
  • 88
  • 118
  • Thanks for your concise answer, Brendan. I have just re-checked that I am running the latest version of Canopy (1.6.2 [64bit]) and have updated all packages. When I start a fresh Canopy session, I receive an error when I run the script you've provided:\n **RuntimeError: A QApplication instance already exists.** with the traceback log pointing to this as a possible culprit: ---> app=QtGui.QApplication(sys.argv) . – thescoop Apr 10 '16 at 10:21
  • when running the script from a command prompt on a windows10 machine, I get an error message : **AttributeError 'module' object has no attribute 'HBoxLayout'** – thescoop Apr 10 '16 at 10:28
  • I get the same error on my mac when running from iPython. If I run from python only on my mac, I get a 'no module PySide' which obviously means my Mac isn't using Canopy's python by default (which prior to now I thought it would be!!!). – thescoop Apr 10 '16 at 10:49
  • Forgot a "Q" in QHBoxLayout, Updated the answer – Brendan Abel Apr 10 '16 at 16:44
  • Still getting this error on canopy : RuntimeError: A QApplication instance already exists ---> app = QtGui.QApplication(sys.argv) – thescoop Apr 10 '16 at 17:01
  • Canopy must already have created a QApplication, you can only have one. – Brendan Abel Apr 10 '16 at 17:05
  • Hey Brendan, It WORKS! When using cmd window in windows and from a terminal on a MAc, but not on Jupyter notebook, nor from the Canopy IDE. Thanks very much! Any ideas on how to kill the first QApplication instance?? – thescoop Apr 10 '16 at 17:37
  • An answer by MountainX covers this issue about QApplications already running ,: http://stackoverflow.com/questions/10888045/simple-ipython-example-raises-exception-on-sys-exit however I've thrown the code into the prog you've given me and it still raises the error. ** app=QtGui.QApplication.instance() # checks if QApplication already exists if not app: # create QApplication if it doesnt exist app = QtGui.QApplication(sys.argv) ** – thescoop Apr 10 '16 at 17:50
  • You can do QApplication.instance() to get the current application if it exists. If it does, don't create another application or call exec. – Brendan Abel Apr 10 '16 at 20:14
  • By default, Canopy's Python Pane runs with %pylab=qt, which is why you are seeing `RuntimeError: A QApplication instance already exists`. You can disable this in Canopy Preferences --> Python tab by unchecking Use Pylab. Does this allow your program to run in Canopy's GUI? – J. Corson Apr 11 '16 at 13:28