2

From my own QGIS plugin, I want to print text to the python console (see motivation below). Most simply, I use

import sys
sys.stdout.write('Outputtext')

And get:

AttributeError: 'NoneType' object has no attribute 'write'

When I open the python console, close it again and start my plugin, it works. Some kind of initialisation routine seems to be missing when used by the plugin.


The background for this - feel free to suggest alternatives

I'd like to use the console as an output for my plugin unit tests:

unittest.TextTestRunner(verbosity=3, stream=sys.stdout).run(suite)

As the console is inside QGIS, it is non-blocking when running the tests (I need to inspect some results in the map, as I will not implement automated checks for all test results that maybe need to be checked. In that sense, it is rather combined unit and integration testing).

Compared to the MessageLog the console provides a better readability (Syntax highlighting.)

3

You can print at Python Console directly with print from your own QGIS plugin. At following image, in my own plugin, I put a print statement in main function (red rectangle) for printing output file name.

enter image description here

When I launch my plugin and select output file name:

enter image description here

after clicking in Save it can be observed that output file name is also printed at Python Console:

enter image description here

  • print() works when I've opened my console window. - Again, it doesn't work on a plain QGIS startup when I haven't opened the console yet. - Furthermore, print() as an output argument for my TextTestRunner writes not the full text output I get with sys.stdout. I assume it's not thought to be used as an output stream like that. – fuzzysolutions Dec 14 '18 at 10:59
0

Workaround that fits my needs - I call:

from console import console
console.show_console()

before I start my TextTestrunner.

See https://github.com/qgis/QGIS/blob/master/python/console/console.py

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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