I'm developing a Python script in which I use several "print" statements to view the intermediate results in Python console of QGIS. Each time after executing the program, I have to manually clear the console using "Clear Console" option.

Is there a way out to clear the printed statements in Python console using a command? I tried the following from this answer


But I am getting this error message:

AttributeError: 'module' object has no attribute 'console'

2 Answers 2


QGIS offers you several classes for logging messages into the Log Messages Panel (i.e., no need to print debug messages from your plugin into the QGIS Python console). You could even easily create a tab in such a panel exclusively for your plugin using QgsMessageLog class:

from qgis.core import Qgis
QgsMessageLog.logMessage( "Info message from plugin", "My Plugin", Qgis.Info )
QgsMessageLog.logMessage( "My warning message", "My Plugin", Qgis.Warning )
QgsMessageLog.logMessage( "My critical message!!!", "My Plugin", Qgis.Critical )
QgsMessageLog.logMessage( "My success message", "My Plugin", Qgis.Success )

Having said that, you can clear the QGIS Python console with these 3 lines:

from qgis.PyQt.QtWidgets import QDockWidget
consoleWidget = iface.mainWindow().findChild( QDockWidget, 'PythonConsole' )
  • 1
    That works !! Thanks for the info regarding Log messages panel
    – Sjs
    Commented Nov 3, 2016 at 13:37
  • 2
    BTW, is there a command available to clear the previously logged messages upon new execution? @Germán Carrillo
    – Sjs
    Commented Nov 9, 2016 at 12:58
  • 1
    Rapidly skimming through the API, I couldn't find such command. Commented Nov 9, 2016 at 13:38

Update. This works independent if the Code Editor is docked:

from PyQt5.QtWidgets import QApplication
from console.console import PythonConsoleWidget

def clear_python_console():
  all_widgets = QApplication.allWidgets()
    for widget in all_widgets:
      if isinstance(widget, PythonConsoleWidget):

Original answer:

The code in Germán Carrilo's answer was working, but today I got:

AttributeError: 'QDockWidget' object has no attribute 'console'

An alternative which working* is:

from qgis.PyQt.QtWidgets import QDockWidget
consoleWidget = iface.mainWindow().findChild(QDockWidget, 'PythonConsole')

*It works only if Dock Code Editor is enabled

enter image description here

Using QGIS 3.34.4


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.