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.)

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Dec 13, 2018 at 9:36

2 Answers 2

4

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

answered Dec 13, 2018 at 14:10
1
  • 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. Commented Dec 14, 2018 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

answered Dec 14, 2018 at 11:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.