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' )
consoleWidget.console.shellOut.clearConsole()
In addition to @bcollins answer, if you want to change your variables you have defined inside the QGIS python console you can try this approach:
Define a variable which holds all variables before you start programming:
my_dir = dir()
Do your magic with gis, for example:
my_variable = 42
my_other_variable = "foo"
Then do something like this:
for varis in dir():
if varis not in my_dir and varis != "my_dir":
print varis
exec("%s = None" % str(varis)) # set them to None
This way you get the all the variables which are NOT in my_dir
and don't remove any other variables that were added by QGIS.
Update:
With the suggestion of @jon_two I try to combine the method of @bcollins and mine. This would be something like:
## after you finished your magic
my_new_dir = list(dir())
for varis in my_new_dir:
if varis not in my_dir and varis != "my_dir":
del locals()[varis]
Best Answer
Try some of the other
CTRL
options that you can find in my answer to Stack Overflow question Stopping python using ctrl+c. There I also explain in depth why certain ones does not work, while others do.