pythonscript: saying that encoding is not defined.



  • @V-S-Rawat,

    Please, if you want our help, format your posts so that code looks like code, rather than getting interpreted by the forum. As we’ve already pointed out, the help can be found by clicking that ? in the COMPOSE window, or by following the link I posted above which gives an excellent summary of how to use markdown in the forum.

    I am using double quotes, not single. like editor.replace(u"क़", u"क़")

    python does not distinguish between single and double quotes, unlike some other languages, so that’s irrelvant (as you discovered later).

    this script was working ever since in npp 7.6.1 32-bit I guess,
    then I switched to npp 7.6.2 64 bit and I noticed that this unicode chars replacements have stopped working.

    Ah, this is useful information: you not only changed between 32-bit and 64-bit, you also changed version. This is likely the culprit. I will have to find time to download portable editions of those, install pythonscript, and see if I can reproduce your problem.

    In the mean time, you example script (even if the source wasn’t clobbered by the forum) is way longer than it needs to be in order to debug the problem. The issue at hand is only trying to set the encoding, so we just need a minimal script that shows the issue.

    In 7.5.8 32-bit, this exact text (copy/paste from the box into a new pythonscript file, then run the python script), will run successfully:

    # encoding=utf-8
    from Npp import console
    console.show()
    console.clear()
    console.write( u'SMILE: ☺\n' )
    console.write( u"क़" )
    

    Where the output is

    SMILE: ☺
    क़
    

    (That was an even-more-simplified version of the script that I had shown earlier, which removes the namespace-protecting function names – but then I used your double-quoted u-string)

    And this version of the script (the exact text shown) will give the error

    # encoding =utf-8
    from Npp import console
    console.show()
    console.clear()
    console.write( u'SMILE: ☺\n' )
    console.write( u"क़" )
    

    where the error I see is

    File "C:\Users\peter.jones\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\NppForumPythonScripts\16899-encoding-sscce.py", line 5
    SyntaxError: Non-ASCII character '\xe2' in file C:\Users\peter.jones\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts\NppForumPythonScripts\16899-encoding-sscce.py on line 5, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
    

    Please try those two exact scripts in your installation(s) of Notepad++ and describe your results.



  • @V-S-Rawat,

    I downloaded portable editions of 7.6.1-32, 7.6.1-64, 7.6.2-32, and 7.6.2-64. I manually installed PythonScript 1.3.0.0 into all four portable installations.

    I ran the two scripts I just showed in all four instances. In all four, the version with # encoding=utf-8 worked, and the version with # encoding =utf-8 failed.

    In 7.6.2-64, I then edited the two scripts to use coding instead of encoding:

    # coding =utf-8
    from Npp import console
    console.show()
    console.clear()
    console.write( u'SMILE: ☺\n' )
    console.write( u"क़" )
    

    This version failed with the same error.

    And the “correct” version:

    # coding=utf-8
    from Npp import console
    console.show()
    console.clear()
    console.write( u'SMILE: ☺\n' )
    console.write( u"क़" )
    

    passed, as it did with encoding.

    Using the encoding lines that are coming through your forum-markdown badly formatted, I cannot reproduce your problem. The only ways I can reproduce your error message are to put a space between coding and =, or by not having the encoding line.

    It is not a problem with PythonScript 1.3.0.0. It is not a problem with my portable versions of 7.6.2 for either 32bit or 64bit.

    Either the text you are quoting is getting mangled – in which case, you will have to correctly use markdown to avoid it getting mangled – or you are doing something else wrong, or there is something else unique about your setup that I cannot reproduce in my portable setup.

    ----
    Complete ? > Debug Info for 7.6.2 64bit

    Notepad++ v7.6.2   (64-bit)
    Build time : Jan  1 2019 - 00:02:38
    Path : C:\usr\local\apps\npp64.7.6.2\notepad++.exe
    Admin mode : OFF
    Local Conf mode : ON
    OS : Windows 10 (64-bit)
    Plugins : DSpellCheck.dll mimeTools.dll NppConverter.dll PythonScript.dll


  • I see that you were also trying without the hyphen in utf8 and with a colon instead of an equal:

    # coding:utf8
    from Npp import console
    console.show()
    console.clear()
    console.write( u'SMILE: ☺\n' )
    console.write( u"क़" )
    

    that one with # coding:utf8 (no space before the colon) passed

    # coding :utf8
    from Npp import console
    console.show()
    console.clear()
    console.write( u'SMILE: ☺\n' )
    console.write( u"क़" )
    

    The one with # coding :utf8 (with the space before the colon) failed.

    Try again with no space between # and coding, and a space after the colon:

    #coding : utf8
    from Npp import console
    console.show()
    console.clear()
    console.write( u'SMILE: ☺\n' )
    console.write( u"क़" )
    

    with space colon space, it fails

    #coding: utf8
    from Npp import console
    console.show()
    console.clear()
    console.write( u'SMILE: ☺\n' )
    console.write( u"क़" )
    

    With nospace colon space, it passes.

    I cannot get the error with the lines you say you are trying. Sorry.

    [these four attempts were still with 7.6.2 64bit portable, as above]



  • your last script gave this output on cosole.
    SMILE: ☺

    it is correct. so it seems that encoding is not the problem.

    thanks for you putting so much time and effort.



  • it worked.

    I had put
    editor.beginUndoAction()
    #coding:utf8

    in my file. meaning coding was not on the first line.

    how I put coding in the first line.

    #coding:utf8
    editor.beginUndoAction()

    the error stopped and it did the required change in my text file.

    I can still say that the previous version was working ever since, but stopped working after I switched to 64 bit and new version.

    maybe, that had change some python version or something that had been causing the error.

    Thanks a lot for guiding me step by step to solution.



  • I’m glad you found the problem.
    Per PEP 263, “To define a source code encoding, a magic comment must be placed into the source files either as first or second line” (emphasis added). That has been true since Python 2.3 in 2001, so it wasn’t a recent change in the Python library. (Besides, since I started using PythonScript a few years ago, they haven’t changed from Python 2.7). I am not sure how it ever would have worked on the third line for you. But the important thing is that you now know it needs to go on the first or second line of your file.



  • @V-S-Rawat said:

    This is my entire script

    editor.beginUndoAction()
    #coding=utf8

    The real problem likely could have been found in like 3 seconds if you would have ever learned how to present code via correct markdown on this forum.

    Scrolling back quickly thru all of the postings shows that only Peter’s code replies use the black-box markdown.



  • Alan Kilborn

    You are right. it troubled all of you and took your time and effort.

    It had caused problem to me also that while the script was not working, I had to find a way around and I had to create a foxreplace for those sets of character, and put everything every time there to do the conversion.

    I don’t know why this site is giving so complicated method of posting.
    what you said ’ ’ ’ will format quote, is not appearing anywhere in the toolbar that appears at post writing.

    just four icons appear in toolbar, bold/ italic are understood, 4th could be link, what is 3rd, (like details in windows) is not clear,
    I fear to click at those icon because they might delete my post written so far.

    even mouse hover tool tip is not appearing on them.

    there is no option for image posting,
    there is no option for code-posting.

    At least they can just give a plain text option of posting, instead of so many categorizations.

    or if such an option is available somewhere deep in setting, please guide me to that.

    site developer should study other sites how simple their post formatting aids are.

    Thanks.



  • @V-S-Rawat said:

    site developer should study other sites how simple their post formatting aids are

    I agree it could be better.



  • @V-S-Rawat

    site developer should study other sites how simple their post formatting aids are.

    feel free to contact the developers of nodebb here ;-)

    but the best thing is if you would read the posts you have been given.

    i quote @PeterJones : … the help can be found by clicking that ? in the COMPOSE window, or by following the link I posted above which gives an excellent summary of how to use markdown in the forum …

    first the ? will lead you to this complete manual on how to markup everything

    second, he took the extra time to extract and post what is relevant for you with an extra explanation and examples, but you sometimes seem to be too stubborn to read or try to understand what people tell you.

    please participate, if you want to get your problems solved.



  • @Meta-Chuh said:

    the ? will lead you to this complete manual on how to markup everything

    Yes but quite frankly that sucks. :D

    but you sometimes seem to be too stubborn to read or try to understand what people tell you

    This is a very common problem here; reminds me much of the OP in this thread: https://notepad-plus-plus.org/community/topic/16840/the-strange-behavior-of-the-cursor-in-the-notepad-window


Log in to reply