Python 3 in Canopy 2 - "No module named PyQt4" error

Primary symptom

In Canopy 2.x running Python 3:

Ask matplotlib to display a plot, or otherwise run Python code that creates a GUI window.

You get an error traceback ending with an error such as these:

ImportError: No module named 'PyQt4'

ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5, or PySide package to be installed, but it was not found.

 

Solution for most users - install the PyQt package

  • Open the Canopy Package Manager
  • Go to the "Available" packages tab
  • Type "pyqt" in the search bar at the top of the window.
  • Select the pyqt package, and click "Install". 

(Note that pyqt is licensed GPL. If you work for an organization which prohibits the use of GPL software in this context, read on.)

 

Alternate solution - use TK

For users who are disconnected from the Internet (so cannot use the Package Manager), or are forbidden to use GPL software (so cannot use PyQt), you can use Python's built-in TK GUI toolkit. (Note that Qt is more capable, attractive, and performant than tk. Some software, such as Enthought's Chaco and Mayavi graphics packages, can not use tk.)

A) For commands and scripts run in the Canopy GUI:

In Canopy 2.1 and above:

  • Open the preferences dialog
  • Go to the "Python" tab
  • Set your Pylab backend to "Interactive (tk)"

If you are using Canopy 2.0, thanks for being an early adopter. Please install Canopy 2.1 or above. You can keep you Canopy User Python environment or replace it, your choice, and you'll have some great new features available.

B) For commands and scripts run in ipython terminal at a command prompt: 

  • Start ipython with ipython --matplotlib tk
  • or start ipython, then type the magic command %matplotlib tk

 

Alternate solution -- static matplotlib plots

If you only need static (non-interactive) matplotlib plots (i.e. no GUI window):

  •  Open Canopy's Preferences dialog (in the "Edit" menu on Windows or Linux; in the "Canopy" menu on Mac).
  • In the Python tab, change the PyLab backend setting to "Inline (SVG)".
  • Click "OK".
  • When prompted to restart kernel, click "Restart".

 

Related symptom:

In Canopy 2.x running Python 3:

Try to start the Data Import Tool.

You get an error traceback ending with:

ImportError: cannot import name DataImportApplication

Solution: Install the PyQt package, as described above.

 

Please do not enter support requests in article comments

Please use article comments for suggestions to improve the article. See "How to submit Enthought Canopy/EPD support requests".

Have more questions? Submit a request

Comments

  • Avatar
    Kenneth Mayer

    Great. Thank you!

Powered by Zendesk