• Login
Community
  • Login

No module named Npp

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
pythonscript
32 Posts 4 Posters 7.0k 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.
  • A
    Alina Gubanova
    last edited by Jun 20, 2019, 5:49 PM

    Hi everyone!

    I am trying to convert files in the directory into the UTF-8 format. This is the code:

    import os;
    import sys;
    import Npp;
    from Npp import notepad
    
    filePathSrc="somedirectoty " # Path to the folder with files to convert
    
    for root, dirs, files in os.walk(filePathSrc):
        for fn in files:
            if fn[-4:] == '.txt':
                 notepad.open(root + "\\" + fn)
                 console.write(root + "\\" + fn + "\r\n")
                 notepad.runMenuCommand("Encoding", "Convert to UTF-8 without BOM")
                 notepad.save()
    notepad.close()
    

    So when I try to run it, I get an error message “No module named Npp”. What do you think could be an issue? Thank you!

    A 1 Reply Last reply Jun 20, 2019, 6:15 PM Reply Quote 2
    • A
      Alan Kilborn @Alina Gubanova
      last edited by Jun 20, 2019, 6:15 PM

      @Alina-Gubanova

      In the Pythonscript console window if you type from Npp import notepad, does it succeed?

      If it succeeds, and you then try notepad.new() does a new editing tab open?

      1 Reply Last reply Reply Quote 4
      • A
        Alina Gubanova
        last edited by Jun 20, 2019, 6:21 PM

        Yes, both work. the new tab opens with notepad.new().

        A 1 Reply Last reply Jun 20, 2019, 6:26 PM Reply Quote 1
        • A
          Alan Kilborn @Alina Gubanova
          last edited by Jun 20, 2019, 6:26 PM

          @Alina-Gubanova

          Did you create your script by doing Plugins (menu) > Pythonscript > New Script and then giving a filename? Then to run it Plugins (menu) > Pythonscript > Scripts > pick-your-named-file-from-the-list ?

          1 Reply Last reply Reply Quote 4
          • A
            Alina Gubanova
            last edited by Jun 20, 2019, 6:32 PM

            Okay, so I am pretty sure I did that previously, but I tried creating a new one just now, and it’s not showing up on the list. What would be the best directory to save it and do I have to add anything to the path?

            A 1 Reply Last reply Jun 20, 2019, 6:35 PM Reply Quote 0
            • A
              Alina Gubanova
              last edited by Jun 20, 2019, 6:34 PM

              I have saved the previous ones to the directory in C:…\Notepad++\plugins\PythonScript\scripts

              1 Reply Last reply Reply Quote 0
              • A
                Alan Kilborn @Alina Gubanova
                last edited by Jun 20, 2019, 6:35 PM

                @Alina-Gubanova

                Well, I use a portable version of Notepad++, and the default it comes up with is ...\npp.7.7.bin\plugins\Config\Pythonscripts\scripts\ so if you are using an installed version of N++ it is going to be different but it should be relatively similar.

                You are naming your script with a .py extension, right?

                (I really can’t tell your level of Python expertise from your postings).

                1 Reply Last reply Reply Quote 3
                • A
                  Alina Gubanova
                  last edited by Jun 20, 2019, 6:41 PM

                  Okay, so I was saving it to the default folder then. Found it now, sorry for confusion, had to reload N++ multiple times. The script is automatically saved as .py. Still getting the same mistake.
                  And no worries, my python expertise is not high, so I’d rather make sure that every step is correct.

                  A D 2 Replies Last reply Jun 20, 2019, 7:15 PM Reply Quote 2
                  • A
                    Alan Kilborn @Alina Gubanova
                    last edited by Jun 20, 2019, 7:15 PM

                    @Alina-Gubanova

                    You are in an area where I don’t think I can provide further help, but @dail is typing, so… :)

                    A 1 Reply Last reply Jun 20, 2019, 7:40 PM Reply Quote 0
                    • A
                      Alina Gubanova @Alan Kilborn
                      last edited by Jun 20, 2019, 7:40 PM

                      @Alan-Kilborn thanks for your help anyway!

                      1 Reply Last reply Reply Quote 0
                      • G
                        guy038
                        last edited by guy038 Jun 20, 2019, 7:51 PM Jun 20, 2019, 7:42 PM

                        Hello, @alina-gubanova, @alan-kilborn, and All,

                        Among my numerous portable versions of Notepad++, installed on my laptop, I considered the most recent one, where the Python script was installed => I tested the Alina’s script with Notepad++ v7.6.3 !

                        As this version is located in D:\@@\763\... :

                        • First, I created a folder named D:\@@\763\Test

                        • Then I created two ANSI files, in folder Test

                        • I opened N++ v7.6.3 and the Python console

                        • I used the Python script, below which worked as expected : The two files, in the Test folder, have been saved with the new UTf-8 encoding, without any trouble !

                        import os;
                        import sys;
                        import Npp;
                        from Npp import notepad
                        
                        filePathSrc="D:\\@@\\763\\Test" # Path to the folder with files to convert
                        
                        for root, dirs, files in os.walk(filePathSrc):
                            for fn in files:
                                if fn[-4:] == '.txt':
                                     notepad.open(root + "\\" + fn)
                                     console.write(root + "\\" + fn + "\r\n")
                                     notepad.runMenuCommand("Encoding", "Convert to UTF-8")
                                     notepad.save()
                        notepad.close()
                        

                        and after executing the script, it wrote, on console :

                        Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:22:17) [MSC v.1500 32 bit (Intel)]
                        Initialisation took 125ms
                        Ready.
                        D:\@@\763\Test\File_1.txt
                        D:\@@\763\Test\File_2.txt
                        

                        As you can see, in the script :

                        • Of course, I just changed the value of the variable filePathSrc

                        • I also changed the line :

                                 notepad.runMenuCommand("Encoding", "Convert to UTF-8 without BOM")
                        

                        by the line :

                                 notepad.runMenuCommand("Encoding", "Convert to UTF-8")
                        

                        This detail supposes that @alina-gubanova is using, a very old version of N++ ;-))

                        Best Regards,

                        guy038

                        A 1 Reply Last reply Jun 20, 2019, 7:58 PM Reply Quote 4
                        • D
                          dail @Alina Gubanova
                          last edited by Jun 20, 2019, 7:52 PM

                          @Alina-Gubanova How are you launching the script? Inside of Notepad++?

                          A 1 Reply Last reply Jun 20, 2019, 7:56 PM Reply Quote 1
                          • A
                            Alina Gubanova @dail
                            last edited by Jun 20, 2019, 7:56 PM

                            @dail yes.
                            So these are my steps:
                            Run -> Python (where I saved C:…\Python2.7\Lib\idlelib\idle.py “$(FULL_CURRENT_PATH)”) -> IDLE opens up in a new window -> press F5 -> Traceback (most recent call last): File "C:\Users\agubanova\Notepad++\plugins\PythonScript\scripts\new script.py", line 3, in <module> import Npp; ImportError: No module named Npp

                            A 1 Reply Last reply Jun 20, 2019, 8:29 PM Reply Quote 1
                            • A
                              Alina Gubanova @guy038
                              last edited by Jun 20, 2019, 7:58 PM

                              @guy038 thank you so much for putting your time in such a detailed explanation. I should definitely update my N++ version :)
                              I followed every step of your explanation, and I still get an error. When you Run the script what is the directory that you use?

                              I suspect mine could be wrong. Is it C:…\Python2.7\Lib\idlelib\idle.py “$(FULL_CURRENT_PATH)” ?

                              1 Reply Last reply Reply Quote 2
                              • A
                                Alan Kilborn @Alina Gubanova
                                last edited by Alan Kilborn Jun 20, 2019, 8:29 PM Jun 20, 2019, 8:29 PM

                                @Alina-Gubanova said:

                                So these are my steps

                                You could have said that in the beginning and saved some time. You can’t run a Pythonscript that way. It isn’t going to know about the special “notepad” objects. You have to run it from the Pythonscript menu, as I said earlier.

                                A D 2 Replies Last reply Jun 20, 2019, 9:15 PM Reply Quote 4
                                • G
                                  guy038
                                  last edited by Jun 20, 2019, 9:10 PM

                                  Hi, @alina-gubanova,

                                  Before, describing the tree structure of my D:\@à\763 folder, note two points :

                                  • The install folder of a portable N++ version must be different from, either, C\Program files and C\Program files (x86)

                                  • Any local installation of Notepad++ needs the zero-length file, doLocalConf.xml, along with the executable notepad++.exe

                                  This file, installed by default, assures you that any file created and/or handled by Notepad++, will be located inside the installation folder ;-))


                                  Depending of your system, follow one of these links, below, to download the last portable N++ v7.7.1

                                  https://notepad-plus-plus.org/repository/7.x/7.7.1/npp.7.7.1.bin.zip

                                  https://notepad-plus-plus.org/repository/7.x/7.7.1/npp.7.7.1.bin.x64.zip

                                  Just create any folder in your system and extract the contents of the archive in this folder !

                                  On the other hand, I’m using the PythonScript v1.3.0.0 ( PythonScript_Full_1.3.0.0.zip ) but you’ll find the latest release v1.4.0.0 , below :

                                  https://github.com/bruderstein/PythonScript/releases

                                  Note that, from N++ version v7.6 and above, there’s a new organization of plugins, in the Plugins folder. From now on, any plugin, let’s say myFirstPlugin.dll, must be moved inside a folder named the same way, so myFirstPlugin !


                                  OK, as promised, here is the tree structure of my D:\@@\763 folder :

                                  D:\@@\763
                                  	\
                                  	|
                                  	|-- autoCompletion (folder)
                                  	|                \
                                  	|                |-- ".xml" files
                                  	|
                                  	|
                                  	|-- localization (folder)
                                  	|              \
                                  	|              |-- ".xml" files
                                  	|
                                  	|
                                  	|-- plugins (folder)
                                  	|         \
                                  	|         |
                                  	|         |-- Config (folder)
                                  	|         |        \
                                  	|         |        |
                                  	|         |        |-- Hunspell (folder)
                                  	|         |        |          \
                                  	|         |        |          |-- en_US.aff
                                  	|         |        |          |
                                  	|         |        |          |-- en_US.dic
                                  	|         |        |
                                  	|         |        |
                                  	|         |        |-- PythonScript (folder)
                                  	|         |        |              \
                                  	|         |        |              |-- scripts (folder)
                                  	|         |        |                        \
                                  	|         |        |                         |-- Alina.py ( YOUR script )
                                  	|         |        |
                                  	|         |        |
                                  	|         |        |-- ".ini" files
                                  	|         |        |
                                  	|         |        |
                                  	|         |        |-- nppPluginList.dll
                                  	|         |
                                  	|         |
                                  	|         |-- doc (folder)
                                  	|         |     \
                                  	|         |      |-- PythonScript(folder)
                                  	|         |                     \
                                  	|         |                     |-- _sources (folder)
                                  	|         |                     |
                                  	|         |                     |-- _static  (folder)
                                  	|         |                     |
                                  	|         |                     |-- ".html" files and Miscellaneous files
                                  	|         |
                                  	|         |
                                  	|         |-- DSpellCheck (folder)
                                  	|         |             \
                                  	|         |             |-- DSpellCheck.dll
                                  	|         |
                                  	|         |
                                  	|         |-- mineTools (folder)
                                  	|         |           \
                                  	|         |           |-- mineTools.dll
                                  	|         |
                                  	|         |
                                  	|         |-- NppConverter (folder)
                                  	|         |              \
                                  	|         |              |-- NppConverter.dll
                                  	|         |
                                  	|         |
                                  	|         |-- NppExport (folder)
                                  	|         |           \
                                  	|         |           |-- NppExport.dll
                                  	|         |
                                  	|         |
                                  	|         |-- PythonScript (folder)
                                  	|                        \
                                  	|                        |-- lib (folder)
                                  	|                        |     \
                                  	|                        |      |-- Sub-folders
                                  	|                        |      |
                                  	|                        |      |-- ".py" files
                                  	|                        |
                                  	|                        |
                                  	|                        |-- scripts (folder)
                                  	|                        |         \
                                  	|                        |         |-- Samples (folder)
                                  	|                        |         |         \
                                  	|                        |         |         |-- ".py" scripts
                                  	|                        |         |
                                  	|                        |         |-- startup.py
                                  	|                        |
                                  	|                        |
                                  	|                        |-- PythonScript.dll  ( Version 1.3.0.0 )
                                  	|
                                  	|
                                  	|-- Test
                                  	|      \
                                  	|      |-- File_1.txt
                                  	|      |
                                  	|      |-- File_2.txt
                                  	|
                                  	|
                                  	|
                                  	|-- themes (folder)
                                  	|        \
                                  	|        |-- ".xml" files
                                  	|
                                  	|
                                  	|-- updater (folder)
                                  	|         \
                                  	|         |-- GUP.exe
                                  	|         |
                                  	|         |-- gup.xml
                                  	|         |
                                  	|         |-- libcurl.dll
                                  	|
                                  	|
                                  	|-- doLocalConf.xml
                                  	|
                                  	|
                                  	|-- Notepad++.exe  ( Version 7.6.3 )
                                  	|
                                  	|
                                  	|-- python27.dll
                                  	|
                                  	|
                                  	|-- SciLexer.dll
                                  	|
                                  	|
                                  	|-- Some ".txt" files
                                  	|
                                  	|
                                  	|-- Some ".xml" CONFIGURATION files
                                  

                                  I Just hope that you’ll get some part useful !

                                  Cheers,

                                  guy038

                                  A 1 Reply Last reply Jun 20, 2019, 9:17 PM Reply Quote 3
                                  • A
                                    Alina Gubanova @Alan Kilborn
                                    last edited by Jun 20, 2019, 9:15 PM

                                    @Alan-Kilborn I apologize for the confusion. Followed the steps that were suggested by a coworker, so wasn’t aware of the issue. I’ll try running it that way. Thank you for your time

                                    A 1 Reply Last reply Jun 21, 2019, 12:31 AM Reply Quote 0
                                    • A
                                      Alina Gubanova @guy038
                                      last edited by Jun 20, 2019, 9:17 PM

                                      @guy038 thank you so much for putting time into this and offering some useful advice! I’ll make sure to follow the steps and let you know!

                                      1 Reply Last reply Reply Quote 1
                                      • D
                                        dail @Alan Kilborn
                                        last edited by Jun 20, 2019, 9:58 PM

                                        @Alan-Kilborn said:

                                        @Alina-Gubanova said:

                                        So these are my steps

                                        You could have said that in the beginning and saved some time. You can’t run a Pythonscript that way. It isn’t going to know about the special “notepad” objects. You have to run it from the Pythonscript menu, as I said earlier.

                                        My suspicion was correct :)

                                        A 1 Reply Last reply Jun 21, 2019, 12:26 AM Reply Quote 2
                                        • A
                                          Alan Kilborn @dail
                                          last edited by Jun 21, 2019, 12:26 AM

                                          @dail said:

                                          My suspicion was correct

                                          Yes, except technically the OP was launching it from within Notepad++. But…there are degrees of within, and when you are wanting to do operations with Python tight to the Scintilla and Notepad++ cores (the original script used the notepad object), you have to be REALLY within. :)

                                          • Run menu : sorta within, but mostly not because it is just telling Notepad++ to launch an external process

                                          • Pythonscript menu : really within, because it is using the Python interpreter part of the plugin (and can access the editor (Scintilla) and notepad (N++) objects.

                                          And @dail, I know you know all this … I write it for the edification of other readers. :)

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