Create a Python project with GUI on Mac

with PySide2

Make your Python projects a little bit more presentable and user-friendly by adding GUI.

Please note that this tutorial is created on top os OSX and might not work with other operating systems.

Step 1. Install all library dependencies

7zip

Install 7zip using homebrew. Run the following code in your terminal. For this part, I don’t think the version of 7zip matters.

brew install p7zip
# Version details:
# 7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
# p7zip Version 16.02 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,4 CPUs x64)

LibClang

Download, and extractlibclang. Run the following code in your terminal. Make sure that the version of libclang you are installing is 3.9 or 4.0.

# Download libclang
wget https://download.qt.io/development_releases/prebuilt/libclang/libclang-release_60-mac-clazy.7z
# Extract libclang using previously downloaded p7zip
7z x libclang-release_60-mac-clazy\ \(1\).7z

Add libclang to your Mac environment by editing your ~/.bash_profile (or ~/.bash_rc) Then source it by running source ~/.bash_profile (or source ~/.bash_rc) in your terminal.

Used vi to edit ~/.bash_profile

Xcode

Download, and install XCode via AppStore

Download Xcode via Appstore

CMake

Download cmake from here. The version I used is cmake 3.12.1. You can check the version that you have by running cmake –version in your terminal.

Make sure to drag the installer inside the Applications folder.

Continue the installation with default settings until you reach the window below, then close.

Open your terminal and run

sudo /Applications/CMake.app/Contents/bin/cmake-gui

Step 2. Create a Python virtual environment

This step is not really needed but it is really good practice to create a Python virtual environment for each of your Python projects. Check here for a step by step tutorial on how to manage different versions of Python, and create a virtual on your OSX using pyenv, virtualenv, andvirtualenv-wrapper.

# Create virtual environment with Python 3.6.5
mkvirtualenv --python="PATH/TO/PYTHON3.6.5/python.exe" python-add-gui

Step 3. Install Qt framework using Homebrew

Run the following steps in your terminal.

# Install qt via homebrew
brew install qt
# Switch to version 5.11.1
brew switch qt 5.11.1
# Do this to link qmake with qt
brew link qt5 --force

Step 4. Build PySide2 from source

Clone the PySide2 repository. Git must be installed for this step.

# Download PySide2
git clone --recursive https://code.qt.io/pyside/pyside-setup

Go inside the main project folder, and switch to your preferred PySide2 version. I will be using version 5.11

cd pyside-setup && git checkout 5.11

Take note of the qmakethat comes with theQt installation.

# Run this to check the path of the qmake
# Make sure to run the qmake that comes with your Qt installation
# I will refer to the output as: <PATH/TO/QMAKE>
which qmake


Sample qmake directory

Build, and installPySide2 from source by running the codes below. The following steps might take time to finish.

# Make sure that your virtual environment is activated
# Build PySide2 from source
python setup.py install --qmake=<PATH/TO/QMAKE> --build-tests --ignore-git --jobs=8
# Install PySide2
python setup.py build --qmake=/path/to/qmake --build-tests --ignore-git --jobs=8
Building PySide from source

Step 5. Test if the setup is successful

python examples/widgets/widgets/tetrix.py

Run the above code, and come up with the sample tetrix application.

Stay tuned for the Part 2 of this tutorial where I will make my own Python application with GUI.

Thanks for reading!

Sources


Create a Python project with GUI on Mac was originally published in Python Pandemonium on Medium, where people are continuing the conversation by highlighting and responding to this story.

Leave a Reply

Your email address will not be published. Required fields are marked *