Qt for Python/GettingStarted: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Category:Qt for Python]]                                                                                                                                 
[[Category:Qt for Python]]                                                                                                                                 
== Getting Started ==                                                                                                                                                  
== 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.                     
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.                     
                                                                                      
                                                                                      
After cloning the official repository you must follow the instructions for         
your specific system.                                                                                                                                                   
=== Platform Requirements ===                                                                                                                                           
=== Platform Requirements ===                                                                                                                                           
* '''Python''':                                                                        
* '''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].)
** Python 3.5+ and Python 2.7
* '''Qt''': 5.12 is recommended, but there are Technical Preview wheels for 5.11
*** (Please notice there is a known issue with Python 3.6.0, [https://wiki.qt.io/Qt_for_Python/Considerations read more].)
* '''libclang''': The libclang library (C-bindings), recommended: version 6 for PySide2 5.12.
* '''Qt''':                                                                            
** 5.11 is recommended
* '''libclang''':                                                                          
** The libclang library (C-bindings), version 5.0 or higher is required for building starting from the PySide2 5.11 branch onwards (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].
** Prebuilt versions of it can be downloaded from [http://download.qt.io/development_releases/prebuilt/libclang/ download.qt.io].
* '''CMake (version >= 3.1 required) ''':                                                                          
* '''CMake (version >= 3.1 required) ''': The build system required by for building PySide2.
** The build system required by for building PySide2.
 
=== Pre-built Wheels ===


=== Install wheel from PyPi ===
Official release wheels of Qt For Python can be installed regularly via pip:
Official release wheels of Qt For Python can be installed regularly via pip:


pip install pyside2
    pip install PySide2


Pre-release (snapshot) wheels containing the latest code changes are available at http://download.qt.io/snapshots/ci/pyside/
'''''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''


For example you can install the latest 5.11 snapshot wheel using:
=== Install wheel from Qt servers ===


pip install --index-url=http://download.qt.io/snapshots/ci/pyside/5.11/latest/ pyside2 --trusted-host download.qt.io
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


=== Building PySide2 ===                                                                                         
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.                                                                                                                 
The building processes are covered in the platform pages.                                                                                                                 
* [[Qt_for_Python_GettingStarted/Windows|Windows]]
* [[Qt_for_Python_GettingStarted/Windows|Windows]]
Line 35: Line 32:
* [[Qt_for_Python_GettingStarted/MacOS|macOS]]
* [[Qt_for_Python_GettingStarted/MacOS|macOS]]
* Mobile platforms are currently not supported (iOS, Android)
* 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 ====                                                                                                                                           
==== 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 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).·                                                                                     
Line 45: Line 45:
* ''--reuse-build'': Rebuilds only modified files
* ''--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)
* ''--openssl=C:\Dev\qtdev\OpenSSL-Win64\bin'': Path to OpenSSL's bindir which contains dlls (Only required for Windows PySide2 packages)
* ''--jobs=#'': Number of # processes to use when building
* ''--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).
* ''--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.
* ''--verbose'': Prints all compiler invocations when building the package.


A typical invocation looks like:                                                     
A typical invocation looks like:                                                     
  python setup.py install --build-tests --jobs=4                                                                                                                       
  python setup.py install --build-tests --j 4                                                                                                                       
A successful build can be tested by running an example:                               
A successful build can be tested by running an example:                               
   python examples/widgets/widgets/tetrix.py                                                                                                   
   python examples/widgets/widgets/tetrix.py                                                                                                   
Line 57: Line 57:
   git grep "PySide2 port"
   git grep "PySide2 port"


=== Running Tests ===                                                                 
== Running Tests ==                                                                 
To perform all the available tests, just execute:                                                                                     
To perform all the available tests, just execute:                                                                                     
  python testrunner.py test  > testlog.txt
  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:
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.11\msvc2015_64\bin;%PATH%
  set PATH=E:\Qt\5.12\msvc2015_64\bin;%PATH%


Run only one test(qpainter_test):                                                                                                                                       
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 possible on Linux and macOS hosts only. Before you build pyside2, ensure that the following requirements are met, to be able to build documentation:
This is currently possible on Linux and macOS hosts only. Before you build pyside2, ensure that the following requirements are met, to be able to build documentation:
Line 83: Line 83:
You could also use the ''docrsts'' make target to generate only the reStructuredText files.
You could also use the ''docrsts'' make target to generate only the reStructuredText files.


=== 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.
                                                                                      
                                                                                      
Line 89: Line 89:
                                                                                      
                                                                                      
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.11 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.11 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/py3.6-qt5.11.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/qt511_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

Revision as of 21:57, 17 December 2018

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

This is currently possible on Linux and macOS hosts only. Before you build pyside2, ensure that the following requirements are met, to be able to build documentation:

  • Install libXML2 and libXSLT before building PySide2 (Ubuntu: apt-get install libxml2-dev libxslt1-dev)
  • 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.
  • Install graphviz (including dot) 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())"