[GIS] Qgis Python console print statements not working

pythonqgis

I have a small problem with the Python console in Qgis.
I am working through some introductory tutorials on Python scripting for Qgis. I have a script that loads layers, then queries the canvas for names and loops through the names and prints them, something like this:

print "This print statement does not help"
#
# import layers to canvas
qgis.utils.iface.addVectorLayer("/path/file1.shp", "First file", "ogr")
qgis.utils.iface.addVectorLayer("/path/file2.shp", "Second file", "ogr")
qgis.utils.iface.addRasterLayer("/path/file3.tif", "Third file")
#
# print "This line triggers the print function in the for loop to work"
#
canvas = qgis.utils.iface.mapCanvas()
allLayers = canvas.layers()
# 
for i in allLayers: print i.name() 

The problem is that, unless I print to the console before the for loop but after the imports, the layer names are not printed, the script simply exists cleanly. If I run the script a second time (with the files still in the canvas) the files are imported again and the for loop works, but of course now there are two copies of the files in the canvas.
Is this yet another path problem or a bug or am I expecting too much?

(OSX 10.7.5 and Qgis 2.6.0)

Best Answer

The best way to print out logging in qgis is to use the QgsMessageLog class. This writes to the debug log window rather then the console.

QgsMessageLog.logMessage("message", "name")

In the lower right of the main window you will see a icon that you can press to open the debug log.

Related Question