Community
    • Login

    Version of Python in script

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    7 Posts 3 Posters 2.4k 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.
    • Paul WormerP
      Paul Wormer
      last edited by

      As a NPP learner and a fairly experienced Python user, I started to learn writing NPP scripts. Via “Plugins Admin” I installed “Python Script”. Because I have a standard installation of Python 3.9.2 on my Windows 10 desktop I hoped that “Python Script” would use this 3.9.2 installation. However, my very first NPP script tells me that it is using Python 2.7.18. (This is confirmed by the Python Script Configuration Panel, which I only discovered while writing this message). As you well know there are some significant incompatibilities between Python 2 and Python 3.

      So my question is: Is it possible to hack the plugin so that it will use my existing Python 3 installation? My version of NPP is 8.1.9.2

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @Paul Wormer
        last edited by PeterJones

        @paul-wormer ,

        There is an alpha version of PythonScript which uses a bundled Python 3 DLL instead of a bundled Python 2 DLL – just look at the releases page and grab the most recent alpha.

        But maybe you are misunderstanding the purpose of PythonScript plugin. It is not primarily for doing your “normal” Python programming. For that, you don’t need the plugin, you just type Python in Notepad++ as normal, and then run your script using your own Python interpreter – you can use a plugin like NppExec to run the script in a console inside Notepad++ itself if that’s what you like, but you don’t even need that.

        The purpose of the PythonScript is for automating Notepad++ – like macros, but on steroids: it is using Python 2 as the scripting language for controlling Notepad++. So you’re really just using the Python 2 for the “glue language” for accessing the methods on the notepad and editor objects that they provide. So any differences in syntax would have minimal impact on your pythonscript scripts for NPP. Though, admittedly, sometimes it’s the small differences between what you’re used to and what you have available that make the most annoyances.

        Paul WormerP Michael VincentM 2 Replies Last reply Reply Quote 3
        • Paul WormerP
          Paul Wormer @PeterJones
          last edited by

          @peterjones
          Thank you for your swift answer. I will look at the alpha.

          I’m sorry for not expressing myself more clearly: I’m not looking for a Python IDE. The point is, I like to work with a personalized editor. For many years I worked with IBM-CMS/Xedit and MS/Kedit and over the years I wrote many, many edit macros in Rexx.

          I have no longer access to Xedit and gave up on Kedit, because, unfortunately, it does not support Unicode. So, what I’m looking for, really, is a replacement for my beloved Kedit/Rexx and I hope to find it in the tandem Notepad++/Python. (I will keep on running Python from the cmd line.)

          Paul WormerP 1 Reply Last reply Reply Quote 2
          • Michael VincentM
            Michael Vincent @PeterJones
            last edited by

            @peterjones said in Version of Python in script:

            There is an alpha version of PythonScript which uses a bundled Python 3 DLL instead of a bundled Python 2 DLL – just look at the releases page and grab the most recent alpha.

            @Paul-Wormer

            I’ve been using the Python Script v3.0.7 for some time now and it works great. The plugin finds your system python libraries in its search path. But as @PeterJones says this is primarily for automating N++ with Python.

            Cheers.

            1 Reply Last reply Reply Quote 2
            • Paul WormerP
              Paul Wormer @Paul Wormer
              last edited by

              This post is deleted!
              1 Reply Last reply Reply Quote 2
              • Paul WormerP
                Paul Wormer
                last edited by

                Earlier I wrote that in my Kedit/Rexx days I often used a simple calculator macro and that I liked to share a very simple Python script, which indeed is as short and compact as its Rexx equivalent. Further I wrote that as a complete Npp noobie it took me about 2 hours to write it and assign it to a key combination. I’m very happy with Npp and its Python plugin. Now I present a small update to the same script:

                """
                    -- Simple calculator for use under Notepad++.
                    
                    Select text that contains a valid Python arithmetic expression
                    and execute this script. 
                    
                    Result is inserted between selected text and text following it.
                    
                    I assigned the script to the key combination: `Alt+Num +`
                    
                    Sample expression: (-(1.0 + 2) * 3 + 55.0) / (8**2) = 0.71875
                """
                from Npp import *
                
                expression = editor.getSelText() 
                if len(expression) == 0:
                    notepad.messageBox('No expression selected') 
                else:    
                    try:
                        answer = str(eval(expression))
                    except SyntaxError as err: 
                        console.write(err.args[0]+": "+err.args[1][3]+"\r\n")
                        editor.addText(' *Error* ')
                    else:    
                        editor.addText(' = ' + answer)
                
                1 Reply Last reply Reply Quote 1
                • Paul WormerP
                  Paul Wormer
                  last edited by Paul Wormer

                  Earlier I posted a simple calculator script which is based on Python 2.7. In Python 2.7 an integer division yields an integer (as in Fortran). In Python 3 this was changed to yield a float. If the Python 3 behavior is wanted, one must add to the top of the script: from __future__ import division.

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