Community
    • Login

    Convenience technique when organizing PythonScripts into folders

    Scheduled Pinned Locked Moved General Discussion
    6 Posts 4 Posters 749 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.
    • Alan KilbornA
      Alan Kilborn
      last edited by Alan Kilborn

      So being someone that has a lot of Notepad++ Pythonscripts…I need to apply some organizational techniques to keep them, well, organized. What this means basically is that I like to create folders for them, based upon what they do.

      I’ve probably taken this to a bit of an extreme(!), but here’s what my Plugins > PythonScript > Scripts menu looks like:

      f5befd38-ac0a-4994-8f0e-55a6d9b2f6d2-image.png

      I’ve highlighted in yellow the entries that PythonScript has put there by default; the rest are mine and as you can see, a lot of new folders I’ve created here…(you can tell they are folders by the > off to the right).

      I’ve noticed that if I want to use a script from one of these folders in a script that lives in another folder, by default Notepad++ won’t be able to find it, example:

      import NotepadGetStatusBar in one script can result in this error at runtime (this is a real-world example from the script I show HERE ):

      6aec9efd-a554-48f4-b2d1-dedfb487efd9-image.png

      Now sure, there are ways to avoid this problem by including path information on the import, but, ok, that’s annoying because I have to look up that path, and then what if I move some scripts around later based upon, I don’t know, maybe a better way of categorizing them?

      What I’ve done to avoid this is to add a bit of code to my startup.py so that one script can “find” another, without the need for any additional specification.

      The code looks like this:

      import os
      
      # add scripts folder tree to the sys.path list
      #  (makes it easy to call a script anywhere in the tree by using only the script's base name)
      for (root, dirs, files) in os.walk(notepad.getPluginConfigDir() + r'\PythonScript\scripts', topdown=False):
          if root not in sys.path:
              sys.path.append(root)
      

      I’m showing this in the event that it helps someone else streamline their work and organize their scripts better, with minimal pain in the event they call one script from another.

      Nick BrownN 1 Reply Last reply Reply Quote 4
      • Nick BrownN
        Nick Brown @Alan Kilborn
        last edited by

        @alan-kilborn

        I also use the following to add any python libraries to the PythonScript plugin install as well

        libDir = r'C:\\Notepad++\\plugins\\PythonScript\\lib'
        try:
            sys.path.index(libDir)
        except ValueError:
            sys.path.append(libDir)
        
        1 Reply Last reply Reply Quote 1
        • Alan KilbornA
          Alan Kilborn
          last edited by

          While we’re on this general subject, if I add this code in:

          for p in sys.path:
              if not os.path.isdir(p):
                  print(p)
          

          I see this (but leading paths removed) in the console after Notepad++ starts up:

          ...\npp.8.1.9.3.portable.x64\plugins\Config\PythonScript\lib
          ...\npp.8.1.9.3.portable.x64\plugins\PythonScript\lib\lib-tk
          ...\npp.8.1.9.3.portable.x64\plugins\PythonScript\python27.zip
          ...\npp.8.1.9.3.portable.x64\DLLs
          ...\npp.8.1.9.3.portable.x64\lib
          ...\npp.8.1.9.3.portable.x64\lib\plat-win
          ...\npp.8.1.9.3.portable.x64\lib\lib-tk
          

          Now, I don’t “install” PythonScript, and I don’t need Tk, so I don’t manually install that, but some of these others seem a bit oddball.

          Expecially the DLLs and lib ones, right off of the main N++ folder…

          1 Reply Last reply Reply Quote 1
          • Michael VincentM
            Michael Vincent
            last edited by

            @alan-kilborn said in Convenience technique when organizing PythonScripts into folders:

            but here’s what my Plugins > PythonScript > Scripts menu looks like:

            WOW, that’s a lot of scripts! Do you “share” them anywhere - GitHub for example?

            Cheers.

            Alan KilbornA PeterJonesP 2 Replies Last reply Reply Quote 0
            • Alan KilbornA
              Alan Kilborn @Michael Vincent
              last edited by

              @michael-vincent said in Convenience technique when organizing PythonScripts into folders:

              Do you “share” them anywhere - GitHub for example?

              Well, I share the ones that I think have general usage, or maybe something I’ve developed to answer a question here…and I share them, well, here on the Community.

              I’ve thought about gists on Github, but haven’t gone there with them as yet.

              1 Reply Last reply Reply Quote 1
              • PeterJonesP
                PeterJones @Michael Vincent
                last edited by

                I used to have all of my scripts in a private repo at some svn-based competitor to github that allowed free private repos. I would just check out that repo as my user scripts folder on any machine I’m working on, and automatically get all my scripts wherever I go.

                As I started sharing more of my scripts, I played a bit with gists, but didn’t like that i had two copies of some scripts – one in the private repo and one in the gist.

                I realized that I didn’t have any proprietary/private scripts (and if I did, I could put them in my system scripts folder instead), so I moved the repo to my github, so I can share any of them.

                So it’s not Alan’s collection, but my collection is at https://github.com/pryrt/nppStuff/tree/main/pythonScripts … and the nppCommunity subdirectory is the ones I’ve developed for or snagged from this forum, mostly organized by topic-ID.

                1 Reply Last reply Reply Quote 3
                • Alan KilbornA Alan Kilborn referenced this topic on
                • First post
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors