PythonScript not displaying errors in console
When my python script does not work, I get no message in the open console window. My startup.py looks ok (see below).
I also wrote a script that had only print(“error”) and nothing happens when I run it.
Other scripts (without stdout or errors) do run correctly. Just can’t seem to get anything to the console.
# The lines up to and including sys.stderr should always come first # Then any errors that occur later get reported to the console # If you'd prefer to report errors to a file, you can do that instead here. import sys from Npp import * # Set the stderr to the normal console as early as possible, in case of early errors sys.stderr = console # Define a class for writing to the console in red class ConsoleError: def __init__(self): global console self._console = console; def write(self, text): self._console.writeError(text); # Set the stderr to write errors in red sys.stderr = ConsoleError() # This imports the "normal" functions, including "help" import site # This sets the stdout to be the currently active document, so print "hello world", # will insert "hello world" at the current cursor position of the current document sys.stdout = editor
as long as
sys.stdout = editor
is set, print statement would be written to the active document.
If you want to have it in the console, you would need to change it to
sys.stdout = console
or using a class similar to the ConsoleError.
Concerning the errors, how do you call/run those scripts?
I also wrote a script that had only print(“error”) and nothing happens when I run it
I was going to point out that this SHOULD generate an error, because the Pythonscript plugin uses Python 2.7 and
print("error")is Python 3 syntax. However, I tried it and it does not generate an error, and actually performs the print (irrespective of the destination of the output).
I tried a quick search but could figure out which subversion of 2.7 introduced this feature.
So both print syntaxes are allowed/correct.
found it, print function has been introduced with version 2.6.
both print syntaxes are allowed/correct.
In Python2 (which includes Pythonscript),
print(...)calls the print builtin with a tuple argument (maybe tough to prove with a one-element tuple?). This is easy to see above with a 2-element tuple, above. :-D
Of course, if you follow the advice of the docs you pointed to, and use
from __future__ import print_functionfirst, things are very different:
So really, I was surprised by
print("error")not producing a real Python error, but maybe I shouldn’t have been – it’s just printing a one-element tuple (I think).
At the risk of being torn apart by Claudia the Python guru, that’s my analysis…and maybe this is really too far outside being Notepad++ related. :-D
At the risk of being torn apart by Claudia the Python guru
hahahaha - obviously the guru didn’t understand what you did so who is the guru :-)
Yes, print function is only available when using import …
but no, I guess, the print statement doesn’t treat it as tuple but as str.
>>> type(("message")) <type 'str'> >>> type(("message",)) <type 'tuple'>
But your again write - not notepad++ related. :-)
@Claudia-Frank Yes, it’s working now. i commented out everything concerning ConsoleError and set the stderr and stdout to console. Now everything seems to work properly. Not sure what the ConsoleError class does, or rather when it is triggered, but it was not sending errors to console.
I generally run the scripts by selecting the script through the PythonScript menu. That’s so I can have it work on an active editor text.
the ConsoleError class is responsible for formatting the message differently as a normal message.
If the standard error pipe should be used, than python script uses the write method of this class,
which itself uses the writeError method if the console object. Which basically means, it writes
the message in red.
If the standard output pipe is used and sys.stdout is set to console, then python script uses the
console.write method which doesn’t do a special formatting of the output.
So it looks like the rest of the console window.
it seems vacation was to long. You write posts that’s for sure but you are right about our discussion :-D
@Claudia-Frank I should do some experiments: when that class was not commented out, I got nothing in the console, black or red. It isn’t a problem now, but I’m a bit curious. (Thanks for the tips.)