Skip to content

Getting going with PyQt5

Peter Corke edited this page Jul 16, 2023 · 3 revisions

PyQt5 is cross-platform GUI toolkit, a set of Python bindings for Qt v5.

It is only required if you are running bdedit but it also provides a nice Matplotlib "backend" for the display of bdsim graphical blocks. For this reason, PyQt5 is not specified as a dependency for bdsim since the whole aim of bdsim is to do block diagrams with code not diagrams.

However, if you want to run bdedit you must have PyQt installed. For most platforms installing PyQt5 is as simple as

pip install PyQt5

and you test your install by running

python bdsim/bdedit/pyqt5_hello.py

which results in the following popup

Screenshot 2023-07-09 at 12 53 26 pm

There are lots of online resources related to installing and debugging PyQt5, so I will ignore GitHub Issues posted on this topic.

PyQt5 on Apple Silicon

PyQt5 is meant to be cross-platform but running on Apple Silicon is not quite so straightforward. I found conflicting advice and solutions that didn't work. This is what worked for me:

brew install pyqt@5

and you need to have brew installed (if you don't use it already, it is a super convenient way to install lots of tools from Linux world).

brew installs PyQt5 into a site-packages folder for each of Python 3.9, 3.10 and 3.11. If you are using an older version of Python it's probably time to upgrade.

This is not the site-packages folder used by pip or conda so you need to tell Python about it using an environment variable. The exact path depends on where brew installs PyQt which you can find by asking brew

brew --prefix pyqt5
/opt/homebrew/opt/pyqt@5

To create the environment variable you need to use this path prefix and the version of Python you want this to work for. If you are using virtual environments it should be the version of Python installed into that environment. For example, for the path prefix above, using Python 3.9 and using tcsh/csh syntax the command is:

setenv PYTHONPATH /opt/homebrew/opt/pyqt@5/lib/python3.9/site-packages

while a more generic approach would be

setenv PYTHONPATH `brew --prefix pyqt@5`/lib/python3.9/site-packages

For bash, the command is:

export PYTHONPATH=/opt/homebrew/opt/pyqt@5/lib/python3.9/site-packages

Test your install by running

python bdsim/bdedit/pyqt5_hello.py

If you have issues check out the relevant online resources. I will ignore GitHub Issues posted on this topic.

Note: it may be possible to get brew to install into the Python package folder used by pip or conda but I don't know how to do that.