• Login
Community
  • Login

PythonScript not displaying errors in console

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
console dialogerrorpythonscript
10 Posts 3 Posters 4.8k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W
    William Winder
    last edited by Sep 25, 2017, 12:02 AM

    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.

    Any suggestions?

    # 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
    
    C 1 Reply Last reply Sep 25, 2017, 10:20 AM Reply Quote 0
    • C
      Claudia Frank @William Winder
      last edited by Sep 25, 2017, 10:20 AM

      @William-Winder

      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?

      Cheers
      Claudia

      W 1 Reply Last reply Sep 25, 2017, 3:32 PM Reply Quote 1
      • S
        Scott Sumner
        last edited by Sep 25, 2017, 12:12 PM

        @William-Winder said:

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

        C 1 Reply Last reply Sep 25, 2017, 1:07 PM Reply Quote 0
        • C
          Claudia Frank
          last edited by Sep 25, 2017, 12:45 PM

          Hi Scott,

          I tried a quick search but could figure out which subversion of 2.7 introduced this feature.
          So both print syntaxes are allowed/correct.

          Cheers
          Claudia

          1 Reply Last reply Reply Quote 1
          • C
            Claudia Frank @Scott Sumner
            last edited by Sep 25, 2017, 1:07 PM

            Hi Scott,

            found it, print function has been introduced with version 2.6 .

            Cheers
            Claudia

            S 1 Reply Last reply Sep 25, 2017, 1:50 PM Reply Quote 0
            • S
              Scott Sumner @Claudia Frank
              last edited by Sep 25, 2017, 1:50 PM

              @Claudia-Frank

              both print syntaxes are allowed/correct.

              Well… :

              Imgur

              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_function first, things are very different:

              Imgur

              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

              C 1 Reply Last reply Sep 25, 2017, 2:17 PM Reply Quote 1
              • C
                Claudia Frank @Scott Sumner
                last edited by Sep 25, 2017, 2:17 PM

                @Scott-Sumner

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

                Cheers
                Claudia

                1 Reply Last reply Reply Quote 0
                • W
                  William Winder @Claudia Frank
                  last edited by Sep 25, 2017, 3:32 PM

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

                  C 1 Reply Last reply Sep 25, 2017, 5:22 PM Reply Quote 0
                  • C
                    Claudia Frank @William Winder
                    last edited by Sep 25, 2017, 5:22 PM

                    @William-Winder

                    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.

                    @Scott-Sumner,
                    it seems vacation was to long. You write posts that’s for sure but you are right about our discussion :-D

                    W 1 Reply Last reply Sep 25, 2017, 5:51 PM Reply Quote 0
                    • W
                      William Winder @Claudia Frank
                      last edited by Sep 25, 2017, 5:51 PM

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

                      1 Reply Last reply Reply Quote 0
                      7 out of 10
                      • First post
                        7/10
                        Last post
                      The Community of users of the Notepad++ text editor.
                      Powered by NodeBB | Contributors