There was a time back in 2014 and earlier when PyQT5 installation was not straightforward and needed manual compilation. When searching on Google, still those posts come up on top results. But nothing to worry about, things have changed – it’s now quite simple.
Installation
If you are not already using Homebrew, you should start using it. Once Homebrew is installed, let’s install PyQT5 with this single command:
brew install pyqt5
Verifying Installation
Let’s take a sample PyQT5 code as example and run it. For examples, I usually pick one up from the excellent PyQT tutorials on zetcode.com. Here’s one:
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial In this example, we create a simple window in PyQt5. author: Jan Bodnar website: zetcode.com last edited: January 2015 """ import sys from PyQt5.QtWidgets import QApplication, QWidget if __name__ == '__main__': app = QApplication(sys.argv) w = QWidget() w.resize(250, 150) w.move(300, 300) w.setWindowTitle('Simple') w.show() sys.exit(app.exec_())
Run it using:
python3 filename.py
If you get a nice looking small window – it worked!
Integrating with Pyenv
I am a big fan of pyenv and use it for running different versions and flavours of Python. If you use pyenv too, chances are you have your own version of Python installed through it. However, the brew formula that installs PyQT5 depends on another formula python3
– homebrew’s own Python 3 installation. When we install PyQT5, this formula is used to install the bindings, so the bindings are only available to this particular Python 3 installation and unavailable to our pyenv versions.
We will discuss two potential solutions to this issue.
Switching to system
One simple work around is to use the Python 3 version installed by Homebrew. We can ask pyenv to switch to the system
version whenever we’re doing PyQT5 development.
pyenv global system python3 filename.py
We can create an alias to quickly switch between Python versions. I have this in my .zshrc
:
alias py2="pyenv global 2.7.8" alias py3="pyenv global 3.5.0" alias pysys="pyenv global system"
This way is very quick and simple but we miss the benefits of using pyenv.
Adding Site Packages
Alternatively, we can add the site-packages
for this homebrew installed python 3 to our pyenv installation of python 3. Since both installations were built on the same machine and OS, the bindings should work correctly. We would be using .pth
files to do this.
Let’s first find out the site-packages for the homebrew installation:
brew info python3
We would notice a message like:
They will install into the site-package directory /usr/local/lib/python3.5/site-packages
That is the site-packages
for this version.
Now let’s find our pyenv python3’s local site directory:
# Switch to pyenv python first python -m site --user-site
Now create a homebrew.pth
file in that directory and put the previously found site packages path there.
Let’s create the file:
# In my local site directory for pyenv vim /Users/masnun/.local/lib/python3.5/site-packages/homebrew.pth
And put these contents:
/usr/local/lib/python3.5/site-packages
Save and exit. Now you should be able to just use:
python filename.py