Develop Qt for Python applications

With Qt for Python, you can use Qt 6 API in Python applications. Use the PySide6 modules to gain access to individual Qt modules, such as Qt Core, Qt GUI, and Qt Widgets.

The following sections describe using Qt Creator for developing with Qt for Python:

For more information about developing with Qt for Python, including limitations, see Qt for Python.

Set up PySide6

If you have not installed the required version of PySide6, Qt Creator prompts you to do so when you open a .py file.

{Prompts to install PySide6 and Python language server}

Further, Qt Creator prompts you to install the Python language server that offers services such as code completion and annotations. Select Install to install PySide6 and the language server.

Create Qt for Python applications

You can use wizards to create Qt for Python application projects. The wizards generate a project file, .pyproject, that lists the files in the Python project. They also generate a .py file that has some boilerplate code. In addition, the widget-based UI wizard creates a .ui file that has a Qt Designer form, and the Qt Quick Application wizard creates a .qml file that imports Qt Quick controls.

Note: Before importing UI classes and after editing them, create the Python code from your UI form. In PySide6, run pyside6-uic form.ui -o in the Terminal view.

{Creating Python code in Terminal}

The Window wizard adds code to the source file, without the UI file.

The Empty wizard adds code to the source file, but it does not add any classes, so you need to add and instantiate them yourself.

The .pyproject files are JSON-based configuration files that replace the previously used .pyqtc configuration files. You can still open and use .pyqtc files, but we recommend that you choose .pyproject files for new projects.

Select the Python interpreter

You select the initial Python interpreter when you use the Qt for Python Application wizard templates to create Python projects.

{Define Project Details dialog}

You can see the current Python interpreter on the Edit mode toolbar.

{Python interpreter on the Edit mode toolbar}

You can change the interpreter to use for a particular project in Projects > Run > Interpreter.

{Python run settings}

To see the available interpreters and choose another interpreter, select the current interpreter, and then select Manage Python Interpreters. Or, select Preferences > Python > Interpreters.

{Python Interpreters in Preferences}

You can add and remove interpreters and clean up references to interpreters that you uninstalled, but that still appear in the list. In addition, you can set the interpreter to use by default.

Create a virtual environment

To use a clean Python virtual environment (venv) that is independent of your global Python installation for a Qt for Python project, select the Create new virtual environment check box in the project wizard. Set the directory where to create the environment in Path to virtual environment.

Use Python interactive shell

You can write Python code in the Edit mode. Select REPL on the toolbar to start the Python interactive shell in the Terminal view.

{Python shell in the Terminal view}

To start the shell and import the current file as a module, select REPL Import File. To also import all functions from the file, select REPL Import *.

See also Creating a Qt for Python Application with Qt Widgets and Creating a Qt for Python Application with Qt Quick.

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.