Wednesday, November 02, 2022

Installing QT5 for Python 3.x Ubuntu 20.04

I built a new Ubuntu box and needed to install Qt5 to go along with Python 3.10.6.  After some googling I found a discussion at GITHUB which had instructions that worked for me.  I'm just documenting what I did so I can repeat the process when needed.  

Open a Terminal Window so that we can install the needed components.  In the Terminal Window enter the following commands one at a time in the order that they appear.  Let each command finish before moving to the next command:  

sudo apt install python3-pip

pip3 install --user pyqt5  

sudo apt-get install python3-pyqt5  

sudo apt-get install pyqt5-dev-tools

sudo apt-get install qttools5-dev-tools


After all five commands above have finished we need to add two lines to the configuration file /usr/lib/x86_64-linux-gnu/qt-default/qtchooser/default.conf .   To do this, open the default.conf file for editing by executing the below sudo gedit command in a Terminal Window, then copy and paste the two lines below to the bottom of the default.conf file:

sudo gedit /usr/lib/x86_64-linux-gnu/qt-default/qtchooser/default.conf

/usr/lib/x86_64-linux-gnu/qt5/bin
/usr/lib/x86_64-linux-gnu


NOTE: if one or both of these lines are already in your default.conf file do NOT duplicate that line.  Just add the line or lines that are NOT in the default.conf file. 


Now we need to create the uic.py file. To do this open a text editor (like gedit) and copy and paste the below text into the into this new file.  Once you've copied and pasted the below text into the new file we save it as uic.py.  Save the uic.py file to your home directory (I saved mine to a directory called bin in my home directory because bin is in my PATH).  

#!/usr/bin/python3

import subprocess
import sys

child = subprocess.Popen(['pyuic5' ,'-x',sys.argv[1]],stdout=subprocess.PIPE)

print(str(child.communicate()[0],encoding='utf-8'))


Once you've save the uic.py file, open a Terminal Window and navigate to where you saved the uic.py file (probably in your home directory).  Once there we need to change the Permissions on the uic.py file to allow execution.  Use the below command in your Terminal Window to do this. Make sure you are in the same directory as your uic.py file in your Terminal Window.

chmod +x uic.py


Now we need to create a symlink.  To do this open a Terminal Window and navigate to the directory where you saved the uic.py file (probably your HOME directory).  Once there type the following command into your Terminal Window:

sudo ln uic.py "/usr/lib/x86_64-linux-gnu/qt5/bin/uic"


Last we will create a Desktop Link so that Qt5 Designer shows up in your App Menu.  To do this open a text editor (like gedit or geany) and copy and paste the below text into the editor.  Once you've copied and pasted the below text into your text editor save the file as qtdesigner.desktop in the directory  /.local/share/application/ which is located in your HOME folder (you may have to turn on "show hidden folder" in you file manager to see this directory).

[Desktop Entry]

Name=Qt5 Designer
Icon=designer
Exec=/usr/lib/x86_64-linux-gnu/qt5/bin/designer
Type=Application
Categories=Application
Terminal=false
StartupNotify=true
Actions=NewWindow
Name[en_US]=Qt5 Designer

[Desktop Action NewWindow]
Name=Open a New Window
Exec=/usr/lib/x86_64-linux-gnu/qt5/bin/designer


If all went well you should now have Qt5 and Qt5 Designer installed in your Ubuntu 20.04 running nicely with your your Python 3.x. You should also have the Qt5 Designer App list in your App Menu as a click to launch Application.

Hope this helps as it worked for me.  If you run into trouble please visit the link to the GITHUB discussion on this topic.  Like I said this is just a rewrite of information from that discussion.