Skip to main content

Light-weight python interpreter, easy to embed into Qt applications

Project description

Latest Version Python versions License: MIT Test status

pyqtconsole is a lightweight python console for Qt applications. It’s made to be easy to embed in other Qt applications and comes with some examples that show how this can be done. The interpreter can run in a separate thread, in the UI main thread or in a gevent task.

Installing

Simply type:

pip install pyqtconsole

Or to install a development version from local checkout, type:

pip install -e .

Simple usage

The following snippet shows how to create a console that will execute user input in a separate thread. Be aware that long running tasks will still block the main thread due to the GIL. See the examples directory for more examples.

import sys
from threading import Thread
from PyQt5.QtWidgets import QApplication

from pyqtconsole.console import PythonConsole

app = QApplication([])
console = PythonConsole()
console.show()
console.eval_in_thread()

sys.exit(app.exec_())

Embedding

  • Separate thread - Runs the interpreter in a separate thread, see the example threaded.py. Running the interpreter in a separate thread obviously limits the interaction with the Qt application. The parts of Qt that needs to be called from the main thread will not work properly, but is excellent way for having a ‘plain’ python console in your Qt app.

  • main thread - Runs the interpreter in the main thread, see the example inuithread.py. Makes full interaction with Qt possible, lenghty operations will of course freeze the UI (as any lenghty operation that is called from the main thread). This is a great alternative for people who does not want to use the gevent based approach but still wants full interactivity with Qt.

  • gevent - Runs the interpreter in a gevent task, see the example _gevent.py. Allows for full interactivity with Qt without special consideration (at least to some extent) for longer running processes. The best method if you want to use pyQtgraph, Matplotlib, PyMca or similar.

Features

Customizing syntax highlighting

The coloring of the syntax highlighting can be customized by passing a formats dictionary to the PythonConsole constructer. This dictionary must be shaped as follows:

import pyqtconsole.highlighter as hl
console = PythonConsole(formats={
    'keyword':    hl.format('blue', 'bold'),
    'operator':   hl.format('red'),
    'brace':      hl.format('darkGray'),
    'defclass':   hl.format('black', 'bold'),
    'string':     hl.format('magenta'),
    'string2':    hl.format('darkMagenta'),
    'comment':    hl.format('darkGreen', 'italic'),
    'self':       hl.format('black', 'italic'),
    'numbers':    hl.format('brown'),
    'inprompt':   hl.format('darkBlue', 'bold'),
    'outprompt':  hl.format('darkRed', 'bold'),
    'fstring':    hl.format('darkCyan', 'bold'),
    'escape':     hl.format('darkorange', 'bold'),
})

All keys are optional and default to the value shown above if left unspecified.

Clear console

A local method, named clear(), is available to clear the input screen and reset the line numbering. Enable it by pushing the method into the available namespace in the console:

console.interpreter.locals["clear"] = console.clear

Shell commands

Optionally, commands entered in the console that start with a special character (e.g. ‘!’) will be executed as shell commands. The output of the command will be printed in the console. This feature is disabled by default, but can be enabled by setting the shell_cmd_prefix parameter when creating the console. For example, on a Linux or macOS system, setting shell_cmd_prefix=’!’ and entering !ls -l will list the files in the current directory.

console = PythonConsole(shell_cmd_prefix=True)
IN [0]: !ls -l
OUT[0]: total 16546
        -rw-r--r-- 1 user user      18741 Fen  6  2026  file1.txt
        -rw-r--r-- 1 user user      18741 Feb  6  2026  file2.txt

Prompt String

By default IN [n]: and OUT [n]: are displayed before each input and output line. You can customize this through constructor arguments:

# Including the line numbers:
console = PythonConsole(inprompt="%d >", outprompt="%d <")
# Or just static:
console = PythonConsole(inprompt=">>>", outprompt="<<<")

Credits

This module depends on QtPy which provides a compatibility layer for Qt4 and Qt5. The console is tested under both Qt4 and Qt5.

Changelog

v1.3.0

Date: 17.02.2026

  • migrated from setup.py + setup.cfg to pyproject.toml

  • dropped official support for Python 3.8 and added 3.13 and 3.14 (no code changes)

  • fixed issue with displaying syntax errors for Python 3.13

  • fixed issue with multibyte characters (emojis) breaking the input (#87)

  • added optional built-in method to clear all input (#58)

  • added optional escape character to execute system commands (e.g !ls -l) (#93)

  • added option to change the prompt placeholder strings (#68)

  • fixed issues in string highlighting (#69)

v1.2.3

Date: 19.09.2023

  • fixed indentation and autocomplete conflict (#74)

  • replaced QRegExp for compatibility with QT6 (#76)

v1.2.2

Date: 18.10.2021

  • fixed PyQt warning because of explicit integer type

  • fixed jedi autocomplete because of method rename

v1.2.1

Date: 17.03.2020

  • fix accepting input with AltGr modifier on win10 (#53)

v1.2.0

Date: 17.03.2020

  • add PySide2 compatibility

  • add Ctrl-U shortcut to clear the input buffer

  • use standard QtPy package to provide the compatibility layer

  • hide the cursor during the execution of a python command

  • mimic shell behaviour when using up and down key to go to end of history

  • fix crash when closing the interpreter window of the threaded example

  • disable excepthook on displaying exception

  • write ‘n’ before syntax errors for consistency

Thanks to @roberthdevries and @irgolic for their contributions!

v1.1.5

Date: 25.11.2019

  • fix TypeError in highlighter when called without formats

v1.1.4

Date: 21.11.2019

  • fix AttributeError due to QueuedConnection on PyQt<5.11 (#23)

  • fix exception on import when started within spyder (#26)

  • fix gevent example to incorporate interoperability code for gevent/Qt (#28)

  • fix not waiting for empty line when entering code blocks before applying input (#30)

  • fix TypeError during compilation step on python 3.8

  • allow user to override syntax highlighting color preferences (#29) note that this is provisional API

  • automate release process (#34)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyqtconsole-1.3.0.tar.gz (29.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyqtconsole-1.3.0-py3-none-any.whl (24.3 kB view details)

Uploaded Python 3

File details

Details for the file pyqtconsole-1.3.0.tar.gz.

File metadata

  • Download URL: pyqtconsole-1.3.0.tar.gz
  • Upload date:
  • Size: 29.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyqtconsole-1.3.0.tar.gz
Algorithm Hash digest
SHA256 5e579e84334f6e3ab527ef2ce0928e1343526e715a18c28a644178af2f90c87c
MD5 e1e308bdd0217907be449a633d492dc1
BLAKE2b-256 65ad45c21ed7b3130c461d00b80bb03dd5cadb9e16f5b0c00d928db33804e41a

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyqtconsole-1.3.0.tar.gz:

Publisher: ci.yml on pyqtconsole/pyqtconsole

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyqtconsole-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: pyqtconsole-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 24.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyqtconsole-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9099c48b10c6c04907fab74dcacd14de3ab95173298bea7e2aab2cda69592525
MD5 a50b698cb06c4da347ffe21f6c0aa6cd
BLAKE2b-256 0420ea8a26da0f7a977c9e6331411e23ec0e6e17fcfd32d4346aea7267f1aa51

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyqtconsole-1.3.0-py3-none-any.whl:

Publisher: ci.yml on pyqtconsole/pyqtconsole

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page