Python3 under Windows 10 Pro 64-Bit: include external script Dialogs, How?



  • when I install PythonScript, notepad++ closed itself, and open it again.
    after this, I get access violation error in loading time of the plugin.



  • ok, I download the portable version 8.2.1 version 64-bit for windows 10 pro
    from 16.07.2021.
    All gone. PythonScript could be successfully re-install.
    But I have a problem, yet.
    How does notepad++ find Python2.7 .exe path?
    I set the global PATH environment variable to the directory E:\Python39\ , so notepad++
    should be find python.exe in there.
    I modified the StartupwithPython script, close all windows, re-start notepad++, but
    I have no success. Is the path hard coded into the source ?

    # -*- coding: utf-8 -*-
    from Npp import notepad, console
    import subprocess
    import shlex
    import os
    
    def run_command(command, __cwd):
        
        _startupinfo = subprocess.STARTUPINFO() 
        _startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        process = subprocess.Popen(shlex.split(command),
                                   startupinfo=_startupinfo,
                                   stdout=subprocess.PIPE, 
                                   stderr=subprocess.PIPE, 
                                   cwd=__cwd)
        _stdout, _stderr = process.communicate()
        
        console.show()
        if _stdout: console.write(_stdout)
        if _stderr: console.write(_stderr)
    
    
    file = notepad.getCurrentFilename()
    _cwd, _ = os.path.split(file)
    run_command('"{}" -u "{}"'.format('E:\\Python39\\python.exe', file), _cwd)
    
    Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)]
    Initialisation took 78ms
    Ready.
    Traceback (most recent call last):
      File "E:\NotePad++\plugins\PythonScript\scripts\Samples\StartWithLocalPython.py", line 2, in <module>
        from Npp import notepad, console
    ModuleNotFoundError: No module named 'Npp'
    

    and this is my windows console (cmd.exe) output:

    C:\Users\devlab1>python -V
    Python 3.9.1
    
    C:\Users\devlab1>python -VV
    Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)]
    
    C:\Users\devlab1>pip install npp
    Collecting npp
      Downloading npp-0.0.0.0a0.dev20210526.tar.gz (1.0 kB)
    Using legacy 'setup.py install' for npp, since package 'wheel' is not installed.
    Installing collected packages: npp
        Running setup.py install for npp ... done
    Successfully installed npp-0.0.0.0a0.dev20210526
    
    C:\Users\devlab1>
    

    You can see the different file date versions?
    Can you check this please ?
    Can you assists me, to bring up PythonScript running?

    I wonder me, why you write about PythonScript.dll, and python27.dll.
    I could not be change any settings - or did I wrong?

    Thanks Jens



  • @Jens-Kallup said in Python3 under Windows 10 Pro 64-Bit: include external script Dialogs, How?:

    How does notepad++ find Python2.7 .exe path?

    It doesn’t. As I said before, PythonScript 1.5.4 comes bundled with a python27.dll – which it stores alongside the plugin’s PythonScript.dll – it uses this DLL for running the python code; it doesn’t use a .exe at all.



  • @Jens-Kallup

    StartupwithPython script

    I had never looked at that script in the sample script folder before. That’s something separate from what I’ve been describing.

    When you use Plugins > Python Script > Scripts > namehere, it will run that namehere.py script using the plugin’s python27 interpreter, and any libraries that the DLL can see (which includes all the overhead for the Npp objects).

    If you use Plugins > Python Script > Scripts > Samples > StartWithLocalPython, then it will launch a new process using the specified python executable (and the libraries that it can see, which don’t include the Npp objects) and send the currently-active file to that python process.



  • here you can see the problem that i have:
    https://imgur.com/UvSb7ML



  • From the current discussion I get the impression that you think you can execute python3 script via the PythonScript interface, this is not true.
    Let me explain.

    PythonScript installed via the plugin admin installs, as Peter already said, Python2.7 and hence is only able to run Python2
    script via its menu interface. The StartwithLocalPython script is trick to start the python3 executable and provide the file
    currently used as a parameter. Means you have to execute always StartWithLocalPython and your script must be the current
    active one.

    Does this makes sense?



  • 8b1625ce-645c-41cd-8e9d-9cc82620f9e5-image.png

    I have made test_python3.py the active document and run StartWithLocalPython via a mapped F5 shortcut.
    There I selected my Python3 installation and then got the result in the PythonScript console.

    Instead the script test_python2.py can be executed directly from the script menu.

    I hope this makes it a bit clearer.



  • ok, thank you. It work very well.
    But, I can’t add short cut for execute the script.
    I open the key mapper, but I can not add a “new item”
    I had this on notepad++ < 8.1.2, but in current version, I missing the new line
    that is empty, when you double the last line.



  • @Jens-Kallup said in Python3 under Windows 10 Pro 64-Bit: include external script Dialogs, How?:

    I can’t add short cut for execute the script.

    You can. You have to use the Python Script > Configuration dialog, and add your script to the “Menu Items” list on the middle-left of that dialog. Then, after re-starting Notepad++, you can assign a keyboard shortcut to that entry using Shortcut Mapper. That isn’t controlled by Notepad++, and has been true for all the years I’ve been using Python Script, and is documented in the Python Script help document (Python Script > Context-Help > Plugin Installation and Usage > Usage)



  • ok, thank you very much of your reply.
    I could create the key shortcut , and I could execute external file Test1.py from Time.py
    this is Time.py:

    from Npp import notepad, console
    
    import subprocess
    import shlex
    import os
    
    def run_command(command, __cwd):
        
        _startupinfo = subprocess.STARTUPINFO() 
        _startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        process = subprocess.Popen(shlex.split(command),
                                   startupinfo=_startupinfo,
                                   stdout=subprocess.PIPE, 
                                   stderr=subprocess.PIPE, 
                                   cwd=__cwd)
        _stdout, _stderr = process.communicate()
        
        console.show()
        if _stdout: console.write(_stdout)
        if _stderr: console.write(_stderr)
    
    file = "E:/NotePad++/plugins/PythonScript/scripts/paule32/test1.py"
    _cwd, _ = os.path.split(file)
    run_command('"{}" -u "{}"'.format('E:/Python39/python.exe', file), _cwd)
    

    this is Test1.py:

    from Npp import notepad, console
    
    import datetime
    import tkinter as tk
    tk._test()
    

    But now, I get Npp error at execute short-cut key:

    Traceback (most recent call last):
      File "E:\NotePad++\plugins\PythonScript\scripts\paule32\test1.py", line 1, in <module>
        from Npp import notepad, console
    ModuleNotFoundError: No module named 'Npp'
    

    so, the Npp module ist installed.

    here my steps, I did:

    E:\NotePad++\plugins\PythonScript\scripts\paule32>pip3 install npp
    Requirement already satisfied: npp in e:\python39\lib\site-packages (0.0.0.0a0.dev20210526)
    
    E:\NotePad++\plugins\PythonScript\scripts\paule32>dir
     Datenträger in Laufwerk E: ist devlab1
     Volumeseriennummer: E0B0-DD56
    
     Verzeichnis von E:\NotePad++\plugins\PythonScript\scripts\paule32
    
    21.07.2021  22:24    <DIR>          .
    21.07.2021  22:24    <DIR>          ..
    22.07.2021  12:39             2.591 test1.py
    21.07.2021  22:33             3.512 Time.py
                   2 Datei(en),          6.103 Bytes
                   2 Verzeichnis(se), 472.926.322.688 Bytes frei
    
    E:\NotePad++\plugins\PythonScript\scripts\paule32>E:\NotePad++\plugins\PythonScript\scripts\paule32>e:\Python39\python.exe -u time.py
    
    Traceback (most recent call last):
      File "E:\NotePad++\plugins\PythonScript\scripts\paule32\time.py", line 5, in <module>
        from Npp import notepad, console
    ModuleNotFoundError: No module named 'Npp'
    
    E:\NotePad++\plugins\PythonScript\scripts\paule32>
    

    after this, I test in mingw64 environment:

    devlab1@rk1700 MINGW64 ~
    $ python3 -VV
    Python 3.8.9 (default, Apr 13 2021, 15:54:59)  [GCC 10.2.0 64 bit (AMD64)]
    
    devlab1@rk1700 MINGW64 /e/Notepad++
    $ cd plugins/PythonScript/scripts/paule32/
    
    devlab1@rk1700 MINGW64 /e/Notepad++/plugins/PythonScript/scripts/paule32
    $ ls
    Time.py  test1.py
    
    devlab1@rk1700 MINGW64 /e/Notepad++/plugins/PythonScript/scripts/paule32
    $ where python3
    E:\msys64\mingw64\bin\python3.exe
    E:\msys64\usr\bin\python3.exe
    
    $ python3 -u time.py
    Traceback (most recent call last):
      File "time.py", line 5, in <module>
        from Npp import notepad, console
    ModuleNotFoundError: No module named 'Npp'
    
    $ pip3 install npp
    Collecting npp
      Downloading npp-0.0.0.0a0.dev20210526.tar.gz (1.0 kB)
    Using legacy 'setup.py install' for npp, since package 'wheel' is not installed.
    Installing collected packages: npp
        Running setup.py install for npp ... done
    Successfully installed npp-0.0.0.0a0.dev20210526
    
    $ pip3 install wheel
    Collecting wheel
      Downloading wheel-0.36.2-py2.py3-none-any.whl (35 kB)
    Installing collected packages: wheel
    Successfully installed wheel-0.36.2
    
    $ pip3 install npp
    Requirement already satisfied: npp in /usr/lib/python3.8/site-packages (0.0.0.0a0.dev20210526)
    
    devlab1@rk1700 MINGW64 /e/Notepad++/plugins/PythonScript/scripts/paule32
    $ python -u time.py
    Traceback (most recent call last):
      File "time.py", line 5, in <module>
        from Npp import notepad, console
    ModuleNotFoundError: No module named 'Npp'
    
    

    I used Time.py in notepad++ - Execute - F5:

    E:\Python39\python.exe $(FULL_CURRENT_PATH)

    with this code (file: test2.py):

    #from Npp import notepad, console
    
    import time
    
    text = 'this is Python3 window, called from Notepadd++ 8.1.2'
    print(text)
    time.sleep(5)
    

    all works fine, exception when the first line outcomment: from Npp …
    then shell window will be open, and closed immedltly.

    I think all the need is Npp pip module, but I lost.
    Any ideas?



  • now, I did following steps in mingw64 shell (msys64):

    step 1:

    devlab1@rk1700 MINGW64 /e/Notepad++/plugins/PythonScript/scripts/paule32
    $ pip3 install npp
    Requirement already satisfied: npp in /usr/lib/python3.8/site-packages (0.0.0.0a0.dev20210526)
    WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
    

    step 2:

    devlab1@rk1700 MINGW64 /e/Notepad++/plugins/PythonScript/scripts/paule32
    $ python3 -m venv tutorial-env
    Looking in links: c:\Users\devlab1\AppData\Local\Temp\tmpogdw6g9l
    Processing c:\users\devlab1\appdata\local\temp\tmpogdw6g9l\setuptools-49.2.1-py3-none-any.whl
    Processing c:\users\devlab1\appdata\local\temp\tmpogdw6g9l\pip-20.2.3-py2.py3-none-any.whl
    Installing collected packages: setuptools, pip
    Successfully installed pip-20.2.3 setuptools-49.2.1
    

    step 3:

    $ ls
    Time.py  test1.py  test2.py  tutorial-env
    

    step 4:

    devlab1@rk1700 MINGW64 /e/Notepad++/plugins/PythonScript/scripts/paule32
    $ ls tutorial-env/bin/
    Activate.ps1          easy_install.exe  pip3.exe       python3.exe
    activate              pip.exe           python.exe     python3w.exe
    easy_install-3.8.exe  pip3.8.exe        python3.8.exe  pythonw.exe
    

    step 5:

    $ ./tutorial-env/bin/activate
    

    step 6:

    $ ./tutorial-env/bin/pip install --force-reinstall --no-cache-dir wheel
    Collecting wheel
      Downloading wheel-0.36.2-py2.py3-none-any.whl (35 kB)
    Installing collected packages: wheel
      Attempting uninstall: wheel
        Found existing installation: wheel 0.36.2
        Uninstalling wheel-0.36.2:
          Successfully uninstalled wheel-0.36.2
    Successfully installed wheel-0.36.2
    

    step 7:

    $ ./tutorial-env/bin/pip install --force-reinstall --no-cache-dir Npp
    Collecting Npp
      Downloading npp-0.0.0.0a0.dev20210526.tar.gz (1.0 kB)
    Building wheels for collected packages: Npp
      Building wheel for Npp (setup.py) ... done
      Created wheel for Npp: filename=npp-0.0.0.0a0.dev20210526-py3-none-any.whl size=1366 sha256=32dfdda848022ac67ae37c1476dd119f7d5e8ad85abb89050224f0589683810f
      Stored in directory: C:/Users/devlab1/AppData/Local/Temp/pip-ephem-wheel-cache-e8zs4bz6/wheels/79/c1/16/6c406f5a8626c6aaafde87534c6b9b3b362fabde33701be030
    Successfully built Npp
    Installing collected packages: Npp
      Attempting uninstall: Npp
        Found existing installation: npp 0.0.0.0a0.dev20210526
        Uninstalling npp-0.0.0.0a0.dev20210526:
          Successfully uninstalled npp-0.0.0.0a0.dev20210526
    Successfully installed Npp-0.0.0.0a0.dev20210526
    

    step 8:

    $ ./tutorial-env/bin/python3.exe
    Python 3.8.9 (default, Apr 13 2021, 15:54:59)  [GCC 10.2.0 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    

    then I typed in the following line:

    import Npp
    

    and I get prompted with:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ModuleNotFoundError: No module named 'Npp'
    

    After this all, I did following steps in Windows PowerShell:
    step 1:

    PS E:\> cd Notepad++\plugins\PythonScript\scripts\paule32
    PS E:\Notepad++\plugins\PythonScript\scripts\paule32> .\tutorial-env\bin\Activate
    (tutorial-env) PS E:\Notepad++\plugins\PythonScript\scripts\paule32>
    

    step 2:

    (tutorial-env) PS E:\Notepad++\plugins\PythonScript\scripts\paule32> .\tutorial-env\bin\pip install --force-reinstall --no-cache-dir wheel
    Collecting wheel
      Downloading wheel-0.36.2-py2.py3-none-any.whl (35 kB)
    Installing collected packages: wheel
      Attempting uninstall: wheel
        Found existing installation: wheel 0.36.2
        Uninstalling wheel-0.36.2:
          Successfully uninstalled wheel-0.36.2
    Successfully installed wheel-0.36.2
    (tutorial-env) PS E:\Notepad++\plugins\PythonScript\scripts\paule32>
    

    step 3:

    (tutorial-env) PS E:\Notepad++\plugins\PythonScript\scripts\paule32> .\tutorial-env\bin\pip install --force-reinstall --no-cache-dir Npp
    Collecting Npp
      Downloading npp-0.0.0.0a0.dev20210526.tar.gz (1.0 kB)
    Building wheels for collected packages: Npp
      Building wheel for Npp (setup.py) ... done
      Created wheel for Npp: filename=npp-0.0.0.0a0.dev20210526-py3-none-any.whl size=1366 sha256=13eccf36e448b597024d306a39db9511ec7ed29ca559474c0f5899bd76fde5e5
      Stored in directory: C:\Users\devlab1\AppData\Local\Temp\pip-ephem-wheel-cache-oz3f1eoe\wheels\79\c1\16\6c406f5a8626c6aaafde87534c6b9b3b362fabde33701be030
    Successfully built Npp
    Installing collected packages: Npp
      Attempting uninstall: Npp
        Found existing installation: npp 0.0.0.0a0.dev20210526
        Uninstalling npp-0.0.0.0a0.dev20210526:
          Successfully uninstalled npp-0.0.0.0a0.dev20210526
    Successfully installed Npp-0.0.0.0a0.dev20210526
    (tutorial-env) PS E:\Notepad++\plugins\PythonScript\scripts\paule32>
    

    step 4:

    (tutorial-env) PS E:\Notepad++\plugins\PythonScript\scripts\paule32> .\tutorial-env\bin\python3.exe .\test2.py
    Traceback (most recent call last):
      File ".\test2.py", line 1, in <module>
        import Npp
    ModuleNotFoundError: No module named 'Npp'
    (tutorial-env) PS E:\Notepad++\plugins\PythonScript\scripts\paule32>
    

    so, I am out of my wisedom - what can I do?
    Jens



  • @Jens-Kallup said in Python3 under Windows 10 Pro 64-Bit: include external script Dialogs, How?:

    so, I am out of my wisedom - what can I do?

    Well, you haven’t understood or taken to heart what I previously said. So I doubt what I say now will be understood either.

    pip3 install npp

    I don’t know for sure what gets downloaded if you run pip3 install Npp, but I assume that downloads and installs this “npp” project listed on PyPI – but that PyPI page tells me virtually nothing about that project, and when I manually looked into the .tar.gz, there appears to be nothing in that module. So as far as I know, it has nothing to do with the Notepad++ PythonScript plugin. Installing that module is likely to do nothing to solve your problem (and I think does nothing at all, because I see no actual Python code in that download).

    The Npp object that is distributed with the PythonScript plugin has hooks into the PythonScript.dll – that is, from what I understand, the Python package(s) cannot be separated from the compiled plugin, because it needs code from the compiled plugin to run.

    If you are using PythonScript 1.5.4 to run a script, you must use Python27.dll-compatible syntax. If you use PythonScript 1.5.4 to launch an external process that runs an external python3.exe on the active file, then the active file must use python 3 syntax, and it will not have access to the PythonScript DLL interface – ie, it won’t have the Npp objects or the ability to control Notepad++'s UI or directly access the contents of the editor components in Notepad++. I don’t know how else to say this, but I’ve been trying this whole conversation to help you understand this.

    If you want a Python 3 compatible script to have access to the internals of Notepad++, then you will need to remove PythonScript 1.5.4 plugin, and instead install the alpha version of PythonScript 3.0.8 plugin that was linked above. That PythonScript 3.0.8 plugin provides its own python 3.9.4 interpreter (in the form of a DLL called python39.dll), and scripts that you write for that plugin are required to be in python3 syntax. As @Ekopalypse described earlier, with the PythonScript 3.0.8 plugin installed, it may then be possible to copy over the equal-version python39.dll from your normal python3 installation overtop of the python39.dll for the plugin, and then use Plugins > PythonScript > Configuration’s toggle “Prefer installed python libraries” checkbox on. Once that is checked on, and you exit Notepad++ and re-start, then if I’ve understood the PythonScript documentation correctly, the interpreter run by PythonScript 3.0.8 should be able to see libraries that you’ve installed with pip3 into your normal Python 3 installation. And thus you should be able to load tkinter as @Ekopalypse showed in the early experiments. I do not have an installed copy of Python3, so I cannot confirm that – but @Ekopalypse’s earlier post with the tkinter experiment .

    So, to reiterate in a simple step-by-step set of instructions
    0. Install your python3 and Notepad++ as normal (I assume this is already done, which is why I called it step 0). The python3 installation will need to be python3.9.x

    1. Uninstall any existing PythonScript plugin, because they are likely the wrong one right now
    2. Download the alpha version of PythonScript 3.0.8 from https://github.com/bruderstein/PythonScript/releases/tag/v3.0.8 – I recommend PythonScript_Full_3.0.8.0_x64_PluginAdmin.zip for a 64-bit Notepad++ – and unzip into your Notepad++ installation (based on the directories you show above, I think it will be the E:\Notepad++\Plugins\PythonScript\ directory)
    3. Start Notepad++, and verify that Plugins > PythonScript > About says v3.0.8 and uses python3.9.4
      eb5f33a5-2bd2-4f1f-8331-df5d1b088631-image.png
    4. Run Plugins > PythonScript > Show Console, and try to import tkinter from that console. It will probably say something like:
      >>> import tkinter
      Traceback (most recent call last):
        File "<console>", line 1, in <module>
      ModuleNotFoundError: No module named 'tkinter'
      
    5. Plugins > Python Script > Configuration…
      1. Change the Initialisation pulldown from LAZY to AT STARTUP
      2. Check the checkbox next to ☑ Prefer installed python libraries
      3. OK to exit Configuration dialog
    6. Exit Notepad++
    7. Go into E:\Notepad++\Plugins\PythonScript\ directory
      1. Rename python39.dll to python39.dll.original
      2. Copy python39.dll from your normal python 3 installation (I presume from E:\Python39\python39.dll, based on the path to your python3 executable you mentioned earlier) and paste it into E:\Notepad++\Plugins\PythonScript
        • From what I understand from the Configuration… dialog text: 2e762845-5a3b-4f6e-9860-ec8f7b844a81-image.png
          … by copying the DLL from your installed python into the Plugins\PythonScript folder, the instance that PythonScript uses will now match your installed python, and will know about the standard search path for your installed python. (This is where I’m a little unclear, but I think by using a different python39.dll, the default contents of sys.path list of directories to search will be different, giving preference to the pip3-installed libraries… but I’m not 100% certain on that. @Ekopalypse can correct me where I’ve got my python facts wrong.)
    8. Start Notepad++, and verify that Plugins > PythonScript > About still says v3.0.8 , but now mentions your installed version of python 3.9.x.
      • In my example, I found a DLL for python3.9.6, so that will be obviously different from the 3.9.4 that came with PythonScript 3.0.8:
        34f2d780-dd62-47b5-ae8b-c572569a923b-image.png
        9fed1f86-caf7-4697-967d-2001247f3e66-image.png
        Notice that the About dialog now shows Python 3.9.6, so it’s using the new DLL
    9. Plugins > PythonScript > Show Console
      1. should also show the updated DLL version in the console window.
        Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)]
        Initialisation took 328ms
        Ready.
        
      2. If I’ve understood the documenation and @Ekopalypse’s example correctly, at this point, in the Console, you should be able to type import tkinter, and if tkinter is installed for your e:\python39\ python, then it should load okay at this point, instead of giving an error. (But, like I’ve said, I don’t have a full installation of python3, so I cannot use pip3 to install tkinter to verify that.)
      3. If that’s true, then use **PythonScript a simple test1.py script should work:
        from Npp import notepad, console
        import datetime
        import tkinter as tk
        tk._test()
        
        And at this point, Plugins > Python Script > Scripts > test1 should work for you, and run a script that has access to both tkinter and the PythonScript notepad++ objects


  • Thank You very much.
    This information’s helps me to add PythonScript3 support for Notepad++ 8.1.2 64-Bit for Windows 10 Pro (H20H2).

    Note:
    When I create a “New Script”, this will let Notepad++ to save the .py file to:
    E:/Notepad++/plugins/Config/PythonScript/scripts/test2.py

    This folder seems to be the “user” script directory.
    Because the “system” script folder differs from that.
    And as such, I have to “add” the “user” script(s) to the left side (system) listbox
    to make it possible to make a short-cut key for this script.
    I don’t know why is that so, but t works for me.

    When I restart Notepad++ 8.1.2, the script will be in the system script location
    and I can add a short-cut key with the key maper.

    Thank You very much !!!

    I hope this thread/post can help others, too, to setup Notepad++ 8.1.2 (16-07-2021)
    with Python3 and PythonScript.

    Greetings, Jens



  • @PeterJones

    Complete, detailed, simply good. I don’t know what more could be said now. Perfect.



  • @Jens-Kallup said in Python3 under Windows 10 Pro 64-Bit: include external script Dialogs, How?:

    I don’t know why is that so,

    The PythonScript documentation explains that you have to add it to the menu in order to make a shortcut.

    If that’s not enough of an explanation as to why: it’s because the Notepad++ Shortcut Mapper will only go as deep as Top Level > Next Level > Action for mapping a shortcut to the action; in this case, Top Level is Plugins and Next Level is Python Script, so an Action has to be in the Python Script level of the menu system for the Shortcut Mapper to be able to see it.

    @Ekopalypse ,

    Complete, detailed, simply good.

    Thanks.


Log in to reply