Difference between revisions of "Qt for Python/GettingStarted"

From Qt Wiki
Jump to: navigation, search
(Archlinux renaming AUR packages and adding Yaourt installation)
(Building the Documentation)
(45 intermediate revisions by 6 users not shown)
Line 1: Line 1:
[[Category:PySide]]
+
[[Category:Qt for Python]]                                                                                                                              
== Considerations before starting ==
+
== Installation ==                                                                                                                                                 
 +
You can install PySide2 via [https://pypi.org/project/PySide2/ PyPi], using [https://download.qt.io/official_releases/QtForPython/ Qt-servers] or by building the source package yourself.                   
 +
                                                                                   
 +
=== Platform Requirements ===                                                                                                                                         
 +
* '''Python''': Python 3.5+ and Python 2.7 (Please notice there is a known issue with Python 3.6.0, [https://wiki.qt.io/Qt_for_Python/Considerations read more].)
 +
* '''Qt''': 5.12 is recommended, but there are Technical Preview wheels for 5.11
 +
* '''libclang''': The libclang library (C-bindings), recommended: version 6 for PySide2 5.12.
 +
** Prebuilt versions of it can be downloaded from [http://download.qt.io/development_releases/prebuilt/libclang/ download.qt.io].
 +
* '''CMake (version >= 3.1 required) ''': The build system required by for building PySide2.
  
PySide2 supports Python 2 (recommended: 2.7 onwards, Compatibility module ''six'' installed) and Python 3 (recommended: 3.5 onwards).
+
=== Install wheel from PyPi ===
 +
Official release wheels of Qt For Python can be installed regularly via pip:
  
On Windows, it is recommended to use Python 3 and build with MSVC2015. Python 2 requires building with MSVC2008. The Qt library has to be built with the same version of MSVC as Python and PySide2.
+
    pip install PySide2
  
Currently, Qt 5.6 and Qt 5.9 onwards are supported. Note that the branch of the pyside-setup repository must match that of the Qt version used.
+
'''''Note:''' This process will automatically install '''shiboken2''' (python module) as dependency, but the package '''shiboken2_generator''' will not since it's the standalone binary that can generate Python bindings from a Qt/C++ project. We '''highly''' recommend to build PySide2 from scratch if you want to generate your own Python bindings from a Qt/C++ project, because the linking information will not be present in the shiboken2_generator wheel''
PySide versions following 5.6 (so 5.9 and dev) use a C++ parser based on [http://clang.org/ Clang]).
 
  
The Clang library (C-bindings), version 3.9 or
+
=== Install wheel from Qt servers ===
higher is required for building. Prebuilt versions of it can be downloaded from
 
[http://download.qt.io/development_releases/prebuilt/libclang/ download.qt.io].
 
  
Qt needs to be built with the ''QtXmlPatterns'' module.
+
Official release wheels of Qt for Python can be installed via pip but from Qt servers:
 +
    pip install --index-url=https://download.qt.io/official_releases/QtForPython/ pyside2 --trusted-host download.qt.io
  
=== Development ===
+
Pre-release (snapshot) wheels containing the latest code changes are available at http://download.qt.io/snapshots/ci/pyside/
 +
For example you can install the latest 5.12 snapshot wheel using:
 +
    pip install --index-url=http://download.qt.io/snapshots/ci/pyside/5.12/latest/ pyside2 --trusted-host download.qt.io
  
Development happens in the 5.9 and dev branches of the [http://code.qt.io/cgit/pyside/pyside-setup.git/ pyside-setup repository].
+
=== Building PySide2 from scratch ===                                                                                       
 +
The building processes are covered in the platform pages.                                                                                                              
 +
* [[Qt_for_Python_GettingStarted/Windows|Windows]]
 +
* [[Qt_for_Python_GettingStarted/X11|Linux/X11]]                                                     
 +
* [[Qt_for_Python_GettingStarted/MacOS|macOS]]
 +
* Mobile platforms are currently not supported (iOS, Android)
 +
* Embedded Linux platforms are currently not supported (Raspberry Pi, iMX.6)
 +
After cloning the official repository you must follow the instructions for your specific system.                                                                                                                                                  
  
Build instructions can be found in the sections [[#Building PySide2|below]].
+
==== setup.py build script ====                                                                                                                                         
 +
The script ''setup.py'' in the [http://code.qt.io/cgit/pyside/pyside-setup.git/ top level repository] is used to build and install the PySide2 package. It takes a mode argument (''build'' or ''install'') and several options (more options are documented in setup.py itself).·                                                                                   
 +
The main options are:                                                               
 +
* ''--qmake=/path/to/qmake'': Path to ''qmake'' of the Qt library to be used                   
 +
* ''--cmake=/path/to/cmake'': Path to ''cmake'' binary                                               
 +
* ''--build-tests'': Builds tests along with some helper packages
 +
* ''--ignore-git'': Prevents ''setup.py'' from cloning and checking out the git submodules.
 +
* ''--debug'': Build in Debug mode (some restrictions apply to Windows, see [[Qt_for_Python_GettingStarted/Windows#Build_considerations|Build considerations]])
 +
* ''--reuse-build'': Rebuilds only modified files
 +
* ''--openssl=C:\Dev\qtdev\OpenSSL-Win64\bin'': Path to OpenSSL's bindir which contains dlls (Only required for Windows PySide2 packages)
 +
* ''--j / parallel #'' : Number of # processes to use when building
 +
* ''--standalone'': Copies over the Qt libraries (and other library dependencies) into the PySide2 package to make it work on other machines (on Windows all builds are standalone, even without specifying the command line argument).
 +
* ''--verbose'': Prints all compiler invocations when building the package.
  
The top level repository has the following submodules:
+
A typical invocation looks like:                                                    
* sources/pyside2-examples: [http://code.qt.io/cgit/pyside/examples.git/ Examples]
+
  python setup.py install --build-tests --j 4                                                                                                                      
* sources/pyside2-tools: uic, rcc tools
+
A successful build can be tested by running an example:                            
* wiki: Wiki
+
   python examples/widgets/widgets/tetrix.py                                                                                                
 
+
You can search for working examples by typing                                      
Contributions follow the [[Qt_Project_Guidelines|standard process]].
+
   cd sources/examples                                                      
 
 
Building requires [https://cmake.org/ CMake].
 
 
 
It is helpful to have debug binaries and/or symbols for Python available. On Windows, this is done by choosing ''Customized Installation'' when installing Python and ticking the respective check boxes. On Linux, debug packages can be installed separately. For Ubuntu, the packages python3-dbg, libpython3-dbg provide a debug binary python3-dbg. On macOS you will need to build the Python interpreter with debug symbols by hand.
 
 
 
It is also recommended to use a [http://docs.python-guide.org/en/latest/dev/virtualenvs/ Virtual Environment] for testing to be able to always start from a clean base and avoid issues with write permissions in installations.
 
 
 
On Linux, the command
 
virtualenv -p /usr/bin/python3-dbg testenv
 
creates a Virtual Environment named ''testenv'' for debugging purposes. On Windows, an installation step may be required:
 
python -m pip install virtualenv
 
python -m virtualenv testenv
 
 
 
The Virtual Environment is activated by
 
  source testenv/bin/activate
 
or
 
CALL testenv\Scripts\activate.bat
 
 
 
Before building the first time, the module [http://www.sphinx-doc.org/en/1.4.9/ Sphinx] should be installed into the virtual environment:
 
pip install sphinx
 
 
 
=== Building PySide2 ===
 
 
 
General build instructions can be found in the [http://code.qt.io/cgit/pyside/pyside-setup.git/tree/README.md?h=5.9 README.md] file.
 
 
 
Sample / specific instructions for building on Ubuntu can be found [[#How to build from sources|below]].
 
 
 
==== Requirements ====
 
* Windows, Linux or macOS (Android / iOS / embedded is not supported at the moment)
 
* Qt package from https://www.qt.io or a custom build of Qt (preferably '''Qt 5.9''' or '''Qt 5.6''')
 
* Python from https://www.python.org/downloads/ ('''Python 3.6''' or '''Python 2.7''')
 
* '''GCC''' (Linux), '''Xcode''' (macOS), '''MSVC2015''' (for Python 3 on Windows), MSCVC2008 (for Python 2 on Windows)
 
* CMake from https://cmake.org/download/ ('''>= 3.1''')
 
* libclang (required for the 5.9 and dev branches) from [http://download.qt.io/development_releases/prebuilt/libclang/ download.qt.io] ('''>= 3.9''')
 
* virtualenv (optional but recommended)
 
* Python sphinx package for documentation (optional, pip install sphinx)
 
 
 
==== setup.py build script ====
 
 
 
The script ''setup.py'' in the [http://code.qt.io/cgit/pyside/pyside-setup.git/ top level repository] is used to build and install the PySide2 package. It takes a mode argument (''build'' or ''install'') and several options (more options are documented in setup.py itself).
 
 
 
The main options are:
 
* --qmake=<binary> Path to ''qmake'' of the Qt library to be used
 
* --cmake=<binary> Path to ''cmake''
 
* --build-tests: Creates a directory containing the tests along with some helper packages
 
* --ignore-git: Prevents ''setup.py'' from cloning and checking out the submodules.
 
* --debug: Build in Debug mode
 
* --reuse-build: Rebuilds only modified files (currently does not work for typesystem xml files)
 
* --openssl: Path to OpenSSL
 
* --jobs : Number of threads to use when building
 
 
 
A typical invocation looks like:
 
  setup.py install --build-tests --jobs=4
 
 
 
A successful build can be tested by running an example:
 
   python sources/pyside2-examples/examples/widgets/widgets/tetrix.py
 
 
 
You can search for working examples by typing
 
   cd sources/pyside2-examples
 
 
   git grep "PySide2 port"
 
   git grep "PySide2 port"
  
Note: When doing local builds of Qt on Linux, the environment LD_LIBRARY_PATH needs to be set to point to the location of the Qt library when running examples, as otherwise they are not found by Python. On macOS you will need to set either the DYLD_LIBRARY_PATH or DYLD_FRAMEWORK_PATH environment variables.
+
== Running Tests ==                                                              
 
+
To perform all the available tests, just execute:                                                                                   
=== Running Tests ===
 
 
 
 
  python testrunner.py test  > testlog.txt
 
  python testrunner.py test  > testlog.txt
  
Run only one test(qpainter_test):
+
Note that to successfully run the tests on Windows you need to point the PATH environment variable to the Qt libdir:
 +
set PATH=E:\Qt\5.12\msvc2015_64\bin;%PATH%
  
 +
Run only one test(qpainter_test):                                                                                                                                     
 
  ctest -R qpainter_test --verbose
 
  ctest -R qpainter_test --verbose
  
=== Building the Documentation ===
+
== Building the Documentation ==                                                  
 
 
This is currently unexplored terrain [https://bugreports.qt.io/browse/PYSIDE-363 PYSIDE-363].
 
* The sources are in pyside2/doc
 
* libXML2 and libXSLT should be present when building PySide2 (Ubuntu: apt-get install libxml2-dev libxslt1-dev)
 
* graphviz + dot should be installed
 
* QT_SRC_DIR needs to be set
 
* sphinx should be installed (pip install sphinx)
 
* qdoc3 is used to generate it
 
 
 
=== <span id="How to build from sources"></span>How to build from sources against Qt 5.7, Qt 5.8, Qt 5.9 on Ubuntu 16.04 LTS 64bit ([http://www.acronymfinder.com/Works-on-My-Machine-(WOMM).html WOMM]) ===
 
 
 
==== prerequisites ====
 
Qt 5.7+ (better from http://download.qt.io/official_releases/online_installers/qt-unified-linux-x64-online.run)
 
libclang-release_39-linux-Rhel7.2-gcc5.3-x86_64.7z from http://download.qt.io/development_releases/prebuilt/libclang/
 
 
 
==== step 01 - libclang ====
 
wget http://download.qt.io/development_releases/prebuilt/libclang/libclang-release_39-linux-Rhel7.2-gcc5.3-x86_64.7z
 
7z x libclang-release_39-linux-Rhel7.2-gcc5.3-x86_64.7z
 
 
 
export CLANG_INSTALL_DIR=$PWD/libclang
 
 
 
==== step 02 - getting pyside2 ====
 
git clone --recursive https://codereview.qt-project.org/pyside/pyside-setup
 
cd pyside-setup && git checkout 5.9
 
 
 
''(commit 8fee86dd7b58c13db39c7c354558119a6346fa5a builds fine)''
 
 
 
==== step 03 - building pyside2 ====
 
python setup.py build --qmake=/home/filippo/Qt/5.7/gcc_64/bin/qmake  --openssl=/usr/bin/openssl --build-tests --ignore-git --jobs=4
 
''(change the path accordingly on your system!)''
 
 
 
==== step 04 - install pyside2 in your env ====
 
ln -s /home/filippo/tmp/pyside2/src/pyside-setup/pyside_package/PySide2 /home/filippo/tmp/pyside2/lib/python2.7/site-packages/PySide2
 
ln -s /home/filippo/tmp/pyside2/src/pyside-setup/pyside_package/PySide2.egg-info/ /home/filippo/tmp/pyside2/lib/python2.7/site-packages/PySide2.egg-info
 
''(change the paths accordingly on your system!)''
 
 
 
==== step 05 - play tetrix ====
 
LD_LIBRARY_PATH=/home/filippo/Qt/5.7/gcc_64/lib/ python sources/pyside2-examples/examples/widgets/widgets/tetrix.py
 
''(change the paths accordingly on your system!)''
 
 
 
=== Building PySide2 from Source Packages Provided by Distributions ===
 
 
 
PySide2 is ideally built from a source-based package provided for your platform's package manager (e.g., Arch Linux's [https://wiki.archlinux.org/index.php/pacman pacman], Gentoo Linux's [https://wiki.gentoo.org/wiki/Portage Portage]). If your platform fails to provide such a package, PySide2 may also be manually built from scratch as a fallback.
 
 
 
==== Arch Linux ====
 
 
 
Arch Linux automates PySide2 installation from source via the [https://aur.archlinux.org AUR] package [https://aur.archlinux.org/packages/python-pyside2-git/ ''python-pyside2-git''] as follows:
 
 
 
===== Using git =====
 
 
 
1. Download ''python-pyside2-git''.
 
 
 
git clone https://aur.archlinux.org/python-pyside2-git.git
 
 
 
2. Install ''python-pyside2-git''.
 
 
 
cd python-pyside2-git
 
makepkg -srci
 
 
 
(An installation using Python 2.7 is also possible through the [https://aur.archlinux.org AUR] package [https://aur.archlinux.org/packages/python2-pyside2-git/ ''python2-pyside2-git''] package)
 
 
 
===== Using [https://archlinux.fr/yaourt-en ''Yaourt''] =====
 
 
 
yaourt -S python-pyside2-git
 
 
 
==== Gentoo Linux ====
 
 
 
Gentoo Linux automates PySide2 installation from source via the [https://github.com/leycec/raiagent/tree/master/dev-python/pyside ''pyside-9999:2''] package hosted by the [https://github.com/gentoo/qt official qt overlay] as follows:
 
 
 
* Install [https://wiki.gentoo.org/wiki/Layman layman] (if you haven't already).
 
 
 
emerge layman
 
echo 'source /var/lib/layman/make.conf' >> /etc/portage/make.conf
 
 
 
* Add the [https://github.com/gentoo/qt qt overlay].
 
 
 
layman -a qt
 
 
 
* Synchronize overlays.
 
 
 
layman -S
 
 
 
* Unmask ''pyside-9999:2'' and ''shiboken-9999:2''.
 
 
 
echo '~dev-python/pyside-9999:2 **\n~dev-python/shiboken-9999:2 **' >> /etc/portage/package.accept_keywords
 
 
 
* '''(Optional)''' Enable ''pyside-9999:2'' USE flags. Most USE flags currently supported by the official [https://packages.gentoo.org/packages/dev-python/PyQt5 PyQt5 ebuild] are also supported by the ''pyside-9999:2'' ebuild. For example:
 
  
echo '~dev-python/pyside-9999:2 concurrent designer help testlib widgets -kde -phonon -script -sql -webkit -webchannel -webengine' >> /etc/portage/package.use
 
  
* Install ''pyside-9999:2'' and ''shiboken-9999:2''.
+
{| style="width:100%; margin-top:10px; background:#fcfcfc; border:1px solid #ccc;"
 +
|
 +
'''Note'''
 +
* This is currently possible on Linux and macOS hosts only
 +
* PySide2 needs to be built using make, not ninja.
 +
|}
  
emerge pyside-9999:2
+
Before you build pyside2, ensure that the following requirements are met, to be able to build the documentation:
  
==== Manually on Ubuntu (Debug Build) ====
+
* Install '''libXML2''' and '''libXSLT''' before building PySide2:<code>
 +
apt-get install libxml2-dev libxslt1-dev # Ubuntu
 +
pacman -S libxml2 libxslt # Arch
 +
</code>
 +
* Set <tt>QT_SRC_DIR</tt> with the path to <tt>qtbase</tt>, if you don't want to build documentation based on the Qt sources under <tt><QT_PKG_ROOT>/<QT_VERSION>/Src/qtbase</tt>.<code>
 +
export QT_SRC_DIR=/path/to/qtbase
 +
</code>
 +
* Install '''graphviz''' and '''sphinx''' <code>pip install graphviz sphinx</code>
  
This is useful for debugging into the interpreter as well as into extensions.
+
Once your PySide2 is built, navigate to the <tt>*_build/*_release/pyside2</tt> directory and run: <code>
Roughly the steps are the following:
+
make apidoc
 +
</code>
  
git clone https://github.com/python/cpython python3.6 && cd python3.6 && git checkout 3.6
+
The build first runs <tt>qdoc</tt> on the Qt sources in <tt>$QT_SRC_DIR</tt> to generate the webxml files, which are then parsed by shiboken to generate ''reStructuredText'' files. In the final step, sphinx is run on the rst files to generate HTMLs.
sudo apt-get build-dep python3.5 # (at time of writing 3.6 was not packaged yet, but it doesn't matter, it's just build dependencies)
 
mkdir build_debug && cd build_debug
 
../configure --with-pydebug --enable-shared --prefix=/home/CHANGE_ME/python36_installed LDFLAGS="-Wl,--rpath=/home/CHANGE_ME/python36_installed/lib" # (you can modify the install path to any directory you wish, make sure to change it in the rpath setting as well)
 
make -j4 && make install
 
virtualenv -p /home/CHANGE_ME/python36_installed/bin/python3.6dm py36
 
  
After that you can build PySide2 using the custom Python interpreter, using the virtualenv you created in the last step. Make sure to change the paths to reflect your own directory structure.
+
You could also use the ''docrsts'' make target to generate only the reStructuredText files.
cd /path/to/location/of/pyside/supermodule
 
python setup.py install --qmake=/home/CHANGE_ME/qt56/bin/qmake --cmake=/usr/bin/cmake --openssl=/usr/bin/openssl --debug --jobs=4 --ignore-git --build-tests
 
  
=== Using Qt Creator as a project explorer ===
+
== Using Qt Creator as a project explorer ==                                      
 
Qt Creator 4.0+ can be used to open the PySide and Shiboken CMakeLists.txt files as projects, and thus provide usual IDE features for developing PySide - project file navigation, code completion (C++ only), following symbols under cursor (C++ only), syntax highlighting, locator usage, debugging, etc.
 
Qt Creator 4.0+ can be used to open the PySide and Shiboken CMakeLists.txt files as projects, and thus provide usual IDE features for developing PySide - project file navigation, code completion (C++ only), following symbols under cursor (C++ only), syntax highlighting, locator usage, debugging, etc.
 
+
                                                                                   
 
Currently there is a limitation that Shiboken has to be built first using the terminal, because the installed shiboken CMake packages will have to be specified for the PySide project in Qt Creator.
 
Currently there is a limitation that Shiboken has to be built first using the terminal, because the installed shiboken CMake packages will have to be specified for the PySide project in Qt Creator.
 
+
                                                                                   
The steps for opening the projects in Qt Creator are:
+
The steps for opening the projects in Qt Creator are:                              
# Open pyside-setup/sources/shiboken2/CMakeLists.txt, and specify a 5.6 Qt Kit to be used
+
# Open pyside-setup/sources/shiboken2/CMakeLists.txt, and specify a 5.12 Qt Kit to be used
# Build the project as usual (by pressing the build icon for instance)
+
# Build the project as usual (by pressing the build icon for instance)              
# Open pyside-setup/sources/pyside2/CMakeLists.txt, and specify the same 5.6 Qt Kit
+
# Open pyside-setup/sources/pyside2/CMakeLists.txt, and specify the same 5.12 Qt Kit
# Go to projects tab, and under the Build / CMake section find the Shiboken2_DIR setting. You have to specify the path to the folder where the Shiboken CMake package was installed when you compiled Shiboken from the terminal
+
# Go to projects tab, and under the Build / CMake section find the Shiboken2_DIR setting. You have to specify the path to the folder where the Shiboken CMake package was installed when you compiled Shiboken from the terminal·
# An example path under MacOS is /Users/user/Dev/pyside2-setup/pyside_install/py2.7-qt5.6.1-64bit-debug/lib/cmake/Shiboken2-2.0.0. The path has to be adjusted depending on the user folder name, the version of python and qt, etc
+
# An example path under MacOS is /Users/user/Dev/pyside2-setup/pyside_install/py3.6-qt5.12.1-64bit-debug/lib/cmake/Shiboken2-2.0.0. The path has to be adjusted depending on the user folder name, the version of python and qt, etc
# (Optional) On MacOS you also have to set the ALTERNATIVE_QT_INCLUDE_DIR setting to the Qt kit include path (e.g. /Users/user/Dev/qt56_source/include)
+
# (Optional) On MacOS you also have to set the ALTERNATIVE_QT_INCLUDE_DIR setting to the Qt kit include path (e.g. /Users/user/Dev/qt511_source/include)                                                                                                                                                                                                                                                                                                                                  
 
# Apply the CMake configuration changes (by pressing the button), and you should be able to build PySide
 
# Apply the CMake configuration changes (by pressing the button), and you should be able to build PySide
 +
                                                                                   
 +
Now you can use the project explorer to look through the source cpp files, python files, use the locator feature to open files and file classes / methods, and other features that Qt Creator provides.
  
Now you can use the project explorer to look through the source cpp files, python files, use the locator feature to open files and file classes / methods, and other features that Qt Creator provides.
+
== Troubleshooting / Known Issues ==
 +
                                                                                   
 +
* Qt 5.9 does not work with OpenSSL 1.1                                       
 +
** When doing a custom Qt build (some unspecified versions for now), It is necessary to have an OpenSSL version of 1.0.x, since there are compatibility issues with newer versions of OpenSSL ([https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes#Qt see details])
 +
* PySide2 looks at the system installation if the local Qt version does not have a required module
 +
**The only workaround is to uninstall any module from the system, then PySide2 can look at only the Qt path currently being use.
 +
* Qt packages that directly link to OpenSSL (as opposed to runtime discovery) are not currently supported.
 +
* Make sure that the Python environment location where the PySide2 package will be installed is writable (otherwise you might get various permission denied errors). The install location can be found with 99% probability by running:
 +
python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"

Revision as of 14:18, 29 April 2019

Installation

You can install PySide2 via PyPi, using Qt-servers or by building the source package yourself.

Platform Requirements

  • Python: Python 3.5+ and Python 2.7 (Please notice there is a known issue with Python 3.6.0, read more.)
  • Qt: 5.12 is recommended, but there are Technical Preview wheels for 5.11
  • libclang: The libclang library (C-bindings), recommended: version 6 for PySide2 5.12.
  • CMake (version >= 3.1 required) : The build system required by for building PySide2.

Install wheel from PyPi

Official release wheels of Qt For Python can be installed regularly via pip:

   pip install PySide2

Note: This process will automatically install shiboken2 (python module) as dependency, but the package shiboken2_generator will not since it's the standalone binary that can generate Python bindings from a Qt/C++ project. We highly recommend to build PySide2 from scratch if you want to generate your own Python bindings from a Qt/C++ project, because the linking information will not be present in the shiboken2_generator wheel

Install wheel from Qt servers

Official release wheels of Qt for Python can be installed via pip but from Qt servers:

   pip install --index-url=https://download.qt.io/official_releases/QtForPython/ pyside2 --trusted-host download.qt.io

Pre-release (snapshot) wheels containing the latest code changes are available at http://download.qt.io/snapshots/ci/pyside/ For example you can install the latest 5.12 snapshot wheel using:

   pip install --index-url=http://download.qt.io/snapshots/ci/pyside/5.12/latest/ pyside2 --trusted-host download.qt.io

Building PySide2 from scratch

The building processes are covered in the platform pages.

  • Windows
  • Linux/X11
  • macOS
  • Mobile platforms are currently not supported (iOS, Android)
  • Embedded Linux platforms are currently not supported (Raspberry Pi, iMX.6)

After cloning the official repository you must follow the instructions for your specific system.

setup.py build script

The script setup.py in the top level repository is used to build and install the PySide2 package. It takes a mode argument (build or install) and several options (more options are documented in setup.py itself).· The main options are:

  • --qmake=/path/to/qmake: Path to qmake of the Qt library to be used
  • --cmake=/path/to/cmake: Path to cmake binary
  • --build-tests: Builds tests along with some helper packages
  • --ignore-git: Prevents setup.py from cloning and checking out the git submodules.
  • --debug: Build in Debug mode (some restrictions apply to Windows, see Build considerations)
  • --reuse-build: Rebuilds only modified files
  • --openssl=C:\Dev\qtdev\OpenSSL-Win64\bin: Path to OpenSSL's bindir which contains dlls (Only required for Windows PySide2 packages)
  • --j / parallel # : Number of # processes to use when building
  • --standalone: Copies over the Qt libraries (and other library dependencies) into the PySide2 package to make it work on other machines (on Windows all builds are standalone, even without specifying the command line argument).
  • --verbose: Prints all compiler invocations when building the package.

A typical invocation looks like:

python setup.py install --build-tests --j 4                                                                                                                       

A successful build can be tested by running an example:

 python examples/widgets/widgets/tetrix.py                                                                                                  

You can search for working examples by typing

 cd sources/examples                                                        
 git grep "PySide2 port"

Running Tests

To perform all the available tests, just execute:

python testrunner.py test  > testlog.txt

Note that to successfully run the tests on Windows you need to point the PATH environment variable to the Qt libdir:

set PATH=E:\Qt\5.12\msvc2015_64\bin;%PATH%

Run only one test(qpainter_test):

ctest -R qpainter_test --verbose

Building the Documentation

Note

  • This is currently possible on Linux and macOS hosts only
  • PySide2 needs to be built using make, not ninja.

Before you build pyside2, ensure that the following requirements are met, to be able to build the documentation:

  • Install libXML2 and libXSLT before building PySide2:
    apt-get install libxml2-dev libxslt1-dev # Ubuntu
    pacman -S libxml2 libxslt # Arch
    
  • Set QT_SRC_DIR with the path to qtbase, if you don't want to build documentation based on the Qt sources under <QT_PKG_ROOT>/<QT_VERSION>/Src/qtbase.
    export QT_SRC_DIR=/path/to/qtbase
    
  • Install graphviz and sphinx
    pip install graphviz sphinx
    
Once your PySide2 is built, navigate to the *_build/*_release/pyside2 directory and run:
make apidoc

The build first runs qdoc on the Qt sources in $QT_SRC_DIR to generate the webxml files, which are then parsed by shiboken to generate reStructuredText files. In the final step, sphinx is run on the rst files to generate HTMLs.

You could also use the docrsts make target to generate only the reStructuredText files.

Using Qt Creator as a project explorer

Qt Creator 4.0+ can be used to open the PySide and Shiboken CMakeLists.txt files as projects, and thus provide usual IDE features for developing PySide - project file navigation, code completion (C++ only), following symbols under cursor (C++ only), syntax highlighting, locator usage, debugging, etc.

Currently there is a limitation that Shiboken has to be built first using the terminal, because the installed shiboken CMake packages will have to be specified for the PySide project in Qt Creator.

The steps for opening the projects in Qt Creator are:

  1. Open pyside-setup/sources/shiboken2/CMakeLists.txt, and specify a 5.12 Qt Kit to be used
  2. Build the project as usual (by pressing the build icon for instance)
  3. Open pyside-setup/sources/pyside2/CMakeLists.txt, and specify the same 5.12 Qt Kit
  4. Go to projects tab, and under the Build / CMake section find the Shiboken2_DIR setting. You have to specify the path to the folder where the Shiboken CMake package was installed when you compiled Shiboken from the terminal·
  5. An example path under MacOS is /Users/user/Dev/pyside2-setup/pyside_install/py3.6-qt5.12.1-64bit-debug/lib/cmake/Shiboken2-2.0.0. The path has to be adjusted depending on the user folder name, the version of python and qt, etc
  6. (Optional) On MacOS you also have to set the ALTERNATIVE_QT_INCLUDE_DIR setting to the Qt kit include path (e.g. /Users/user/Dev/qt511_source/include)
  7. Apply the CMake configuration changes (by pressing the button), and you should be able to build PySide

Now you can use the project explorer to look through the source cpp files, python files, use the locator feature to open files and file classes / methods, and other features that Qt Creator provides.

Troubleshooting / Known Issues

  • Qt 5.9 does not work with OpenSSL 1.1
    • When doing a custom Qt build (some unspecified versions for now), It is necessary to have an OpenSSL version of 1.0.x, since there are compatibility issues with newer versions of OpenSSL (see details)
  • PySide2 looks at the system installation if the local Qt version does not have a required module
    • The only workaround is to uninstall any module from the system, then PySide2 can look at only the Qt path currently being use.
  • Qt packages that directly link to OpenSSL (as opposed to runtime discovery) are not currently supported.
  • Make sure that the Python environment location where the PySide2 package will be installed is writable (otherwise you might get various permission denied errors). The install location can be found with 99% probability by running:
python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"