• Login
Community
  • Login

Adding Libraries to PythonScript Plugin

Scheduled Pinned Locked Moved Notepad++ & Plugin Development
40 Posts 5 Posters 8.1k 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.
  • M
    Michael Vincent @John Doe 1
    last edited by Michael Vincent Apr 5, 2022, 4:58 PM Apr 5, 2022, 4:57 PM

    @john-doe-1 said in Adding Libraries to PythonScript Plugin:

    I wish I could get Pandas to work

    For me - it’s dead simple:

    PS VinsWorldcom ~ > python3
    Python 3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pandas
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ModuleNotFoundError: No module named 'pandas'
    

    and PythonScript:

      File "C:\usr\bin\npp64\plugins\Config\PythonScript\scripts\startup.py", line 1 : PythonScript initialized
    Python 3.8.9 (tags/v3.8.9:a743f81, Apr  2 2021, 11:10:41) [MSC v.1928 64 bit (AMD64)]
    Initialisation took 1047ms
    Ready.
    >>> import pandas
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
    ModuleNotFoundError: No module named 'pandas'
    

    So, we install it:

    PS VinsWorldcom ~ > python3 -mpip install pandas
    Collecting pandas
      Downloading pandas-1.4.2-cp38-cp38-win_amd64.whl (10.6 MB)
         |████████████████████████████████| 10.6 MB 6.4 MB/s
    Collecting pytz>=2020.1
      Downloading pytz-2022.1-py2.py3-none-any.whl (503 kB)
         |████████████████████████████████| 503 kB 6.4 MB/s
    Requirement already satisfied: numpy>=1.18.5 in c:\users\VinsWorldcom\appdata\roaming\python\python38\site-packages (from pandas) (1.19.5)
    Requirement already satisfied: python-dateutil>=2.8.1 in c:\users\VinsWorldcom\appdata\roaming\python\python38\site-packages (from pandas) (2.8.1)
    Requirement already satisfied: six>=1.5 in c:\users\VinsWorldcom\appdata\roaming\python\python38\site-packages (from python-dateutil>=2.8.1->pandas) (1.15.0)
    Installing collected packages: pytz, pandas
    Successfully installed pandas-1.4.2 pytz-2022.1
    

    Now it works:

    PS VinsWorldcom ~ > python3
    Python 3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pandas
    >>> print(pandas.__file__)
    C:\Users\VinsWorldcom\AppData\Roaming\Python\Python38\site-packages\pandas\__init__.py
    

    And it now works in PythonScript as well:

      File "C:\usr\bin\npp64\plugins\Config\PythonScript\scripts\startup.py", line 1 : PythonScript initialized
    Python 3.8.9 (tags/v3.8.9:a743f81, Apr  2 2021, 11:10:41) [MSC v.1928 64 bit (AMD64)]
    Initialisation took 1047ms
    Ready.
    >>> import pandas
    >>> print(pandas.__file__)
    C:\Users\VinsWorldcom\AppData\Roaming\Python\Python38\site-packages\pandas\__init__.py
    

    You’ll see, it’s finding the same Pandas - from my system Python.

    Cheers.

    J 1 Reply Last reply Apr 5, 2022, 5:23 PM Reply Quote 2
    • J
      John Doe 1 @Michael Vincent
      last edited by Apr 5, 2022, 5:23 PM

      @michael-vincent Thank you for your further assistance, how do you open the Python.exe with this line above it:

      File “C:\usr\bin\npp64\plugins\Config\PythonScript\scripts\startup.py ”, line 1 : PythonScript initialized

      ?

      So that I can import packages to that environment

      M 1 Reply Last reply Apr 5, 2022, 5:42 PM Reply Quote 0
      • M
        Michael Vincent @John Doe 1
        last edited by Michael Vincent Apr 5, 2022, 5:43 PM Apr 5, 2022, 5:42 PM

        @john-doe-1 said in Adding Libraries to PythonScript Plugin:

        how do you open the Python.exe with this line above it:

        That’s the PythonScript console, menu Plugins => Python Script => Show Console.

        60d1d6bc-24a6-4ca6-90cf-f6ec7388a851-image.png

        My ‘startup.py ’ does some initialization - including printing the file that ran / did the initialization so I can reference it if there is an issue.

        Cheers.

        J 1 Reply Last reply Apr 5, 2022, 5:55 PM Reply Quote 2
        • J
          John Doe 1 @Michael Vincent
          last edited by Apr 5, 2022, 5:55 PM

          @michael-vincent Okay I feel very silly, when I first installed Notepad++ and PythonScripts I deleted the Sample scripts along with startup.py , I just put them back where they belong after inspecting your post.

          Now my simple test scripts invoking Pandas are at least running, that’s a step foward, I will see if Panda’s functionality works as should.

          Thank you very much for the detailed assistance and screenshots, @Michael-Vincent @Alan-Kilborn @PeterJones your help is much appreciated.

          :)

          1 Reply Last reply Reply Quote 2
          • J
            John Doe 1
            last edited by John Doe 1 Apr 5, 2022, 9:15 PM Apr 5, 2022, 9:15 PM

            @michael-vincent Hey I’ve been doing some testing in PyCharm and noticed something strange. I’ve imported pandas and Npp like so:

            from Npp import *
            import pandas as pandasForSortingCSV

            Now with these imports I can read a csv no problem like so:

            file = pandasForSortingCSV.read_csv(notepad.getCurrentFilename())

            BUT if I try to use the “sort_values” function, nothing happens:

            sorted_file = file.sort_values(by=[“Name”], ascending=False, inplace=True)

            If I hover over that statement in PyCharm it says:
            No documentation found.

            Do you happen to have any idea why this might be? The documentation shows that same import supporting the sort_values function so I’m not sure what the issue is. Please let me know if you can think of anything, thank you!

            M M 2 Replies Last reply Apr 6, 2022, 4:52 AM Reply Quote 0
            • M
              mpheath @John Doe 1
              last edited by Apr 6, 2022, 4:52 AM

              @john-doe-1 said in Adding Libraries to PythonScript Plugin:

              @michael-vincent Hey I’ve been doing some testing in PyCharm and noticed something strange. I’ve imported pandas and Npp like so:
              If I hover over that statement in PyCharm it says:
              No documentation found.

              Do you happen to have any idea why this might be? The documentation shows that same import supporting the sort_values function so I’m not sure what the issue is. Please let me know if you can think of anything, thank you!

              sort_values currently has no doc-string . PyCharm would inspect sort_values and finds no documentation. A comment is in the source # TODO: Just move the sort_values doc here.

              Please focus mainly on Notepad++ and PythonScript. This topic is getting very broad if PyCharm issues are included.

              J 1 Reply Last reply Apr 6, 2022, 1:28 PM Reply Quote 1
              • M
                Michael Vincent @John Doe 1
                last edited by Apr 6, 2022, 12:30 PM

                @john-doe-1 said in Adding Libraries to PythonScript Plugin:

                I’ve been doing some testing in PyCharm

                WHY?!?!?!

                The Npp library won’t work in PyCharm and If you’re trying to manipulate CSV files as I thought you were and not doing it in Notepad++, why are we even talking about Notepad++ and PythonScript. You can easily open files from disk in Python without going through Notepad++ if you’re not even using it.

                BUT if I try to use the “sort_values” function, nothing happens:

                sorted_file = file.sort_values(by=[“Name”], ascending=False, inplace=True)

                No kidding - you didn’t do anything to the file in Notepad++, you just tried to sort the file on disk by opening it by name, not referencing the content of the file in the Notepad++ Scintilla editing component by some method of the editor instance.

                If you want to actually sort the file in Notepad++ (and ultimately the one on disk), you need to read the content of the editor object , sort it and write it back.

                Maybe have a look at those example scripts that came with PythonScript. Run them. See how they work. Try modifying them a bit to make them do something else before diving headfirst into full file manipulation with pandas.

                And please for heavens sake backup all of your data before going any further. I fear this won’t end well.

                Cheers.

                J 1 Reply Last reply Apr 6, 2022, 1:17 PM Reply Quote 2
                • J
                  John Doe 1 @Michael Vincent
                  last edited by John Doe 1 Apr 6, 2022, 1:17 PM Apr 6, 2022, 1:17 PM

                  I was only using PyCharm for syntax highlighting and intellisense I know that the Npp library doesn’t work there. I am trying to edit the CSV files in Notepad++

                  @michael-vincent said in Adding Libraries to PythonScript Plugin:

                  If you want to actually sort the file in Notepad++ (and ultimately the one on disk), you need to read the content of the editor object , sort it and write it back.

                  That is what I am trying to do, shouldn’t this line be reading the filename via Notepad. and reading the CSV using read_csv?

                  file = pandasForSortingCSV.read_csv(notepad.getCurrentFilename())

                  A P 2 Replies Last reply Apr 6, 2022, 1:29 PM Reply Quote 0
                  • J
                    John Doe 1 @mpheath
                    last edited by Apr 6, 2022, 1:28 PM

                    @mpheath said in Adding Libraries to PythonScript Plugin:

                    sort_values currently has no doc-string. PyCharm would inspect sort_values and finds no documentation. A comment is in the source # TODO: Just move the sort_values doc here.

                    Where is that comment and where can I access the sort_values doc? Shouldn’t this have been done upon installation of pandas?

                    A 1 Reply Last reply Apr 6, 2022, 1:31 PM Reply Quote 0
                    • A
                      Alan Kilborn @John Doe 1
                      last edited by Apr 6, 2022, 1:29 PM

                      @john-doe-1

                      Nobody here is going to comment on what Pandas’ read_csv function is going to do, because, like a lot of your questions, it is OFF-TOPIC for this forum.

                      Something on-topic:

                      Typically with PythonScripting, you want to act on the live text inside a Notepad++ editing tab. You don’t want to be dealing with files in the OS, if you can avoid it. If you manipulate the disk file, you will get a popup to the effect of “file on disk has changed – do you want to reload it?”, making for an awkward user experience with your script.

                      J 1 Reply Last reply Apr 6, 2022, 1:42 PM Reply Quote 0
                      • A
                        Alan Kilborn @John Doe 1
                        last edited by Apr 6, 2022, 1:31 PM

                        @john-doe-1 said in Adding Libraries to PythonScript Plugin:

                        Where is that comment and where can I access the sort_values doc? Shouldn’t this have been done upon installation of pandas?

                        More OFF-TOPIC stuff.
                        Please try to confine discussed issues to on-topic things.

                        1 Reply Last reply Reply Quote 0
                        • J
                          John Doe 1 @Alan Kilborn
                          last edited by Apr 6, 2022, 1:42 PM

                          @alan-kilborn said in Adding Libraries to PythonScript Plugin:

                          Typically with PythonScripting, you want to act on the live text inside a Notepad++ editing tab. You don’t want to be dealing with files in the OS, if you can avoid it.

                          That is what I am trying to do, I suppose the bet way would be to get all of the text in the currently open file using the editor object somehow…

                          A 1 Reply Last reply Apr 6, 2022, 1:45 PM Reply Quote 0
                          • P
                            PeterJones @John Doe 1
                            last edited by PeterJones Apr 6, 2022, 1:46 PM Apr 6, 2022, 1:45 PM

                            @john-doe-1,

                            We cannot help with Pandas-specific questions. But the phrasing and example code shows you don’t understand the PythonScript plugin interface yet, nor the difference between a file on disk and the file that’s open in Notepad++.

                            When Notepad++ opens a file, it reads the contents from disk, and stores it in memory. When you edit the file in Notepad++, you are editing the file in memory until you (or some automated task) saves.

                            You said,

                            That is what I am trying to do, shouldn’t this line be reading the filename via Notepad. and reading the CSV using read_csv?
                            file = pandasForSortingCSV.read_csv(notepad.getCurrentFilename())

                            No. That is looking up the name of the file that is currently open in Notepad++, and passing that filename as an argument to a Pandas command. Presumably, the Pandas command separately reads the file based on its filename from disk; it will not read the contents from the file that’s open in Notepad++. So if you had edited-but-not-saved in Notepad++, Pandas will read whatever’s on disk, not what’s open in the Notepad++ window.

                            While I was typing this reply, Alan and you had a back and forth, in which you said,

                            I suppose the bet way would be to get all of the text in the currently open file using the editor object somehow…

                            That’s what I was working toward. editor.getText() will give you the entire contents of the currently-active editor tab as a string.

                            If there is a version of the Pandas.read_csv or similar command that will accept a string that contains the contents of the CSV, instead of a filename, then you can send the that string to the function… pandasForSortingCSV.read_csv_from_string(editor.getText()) . (Please understand, I am not saying that read_csv_from_string exists… I am using that as a dummy function; you will have to learn enough Pandas to know how to read a CSV from a string instead of from a file name. We cannot help you with Pandas here)

                            A 1 Reply Last reply Apr 6, 2022, 1:47 PM Reply Quote 3
                            • A
                              Alan Kilborn @John Doe 1
                              last edited by Apr 6, 2022, 1:45 PM

                              @john-doe-1 said in Adding Libraries to PythonScript Plugin:

                              get all of the text in the currently open file using the editor object somehow

                              Well, that “somehow” is probably all_text = editor.getText(). :-)

                              It’s a basic concept, but it is on-topic, so it is all good. :-)

                              1 Reply Last reply Reply Quote 3
                              • A
                                Alan Kilborn @PeterJones
                                last edited by Apr 6, 2022, 1:47 PM

                                @peterjones said in Adding Libraries to PythonScript Plugin:

                                We cannot help you with Pandas here

                                Well, I could as I am very familiar with Pandas…but I refuse to help with it here. :-)

                                J 1 Reply Last reply Apr 6, 2022, 2:10 PM Reply Quote 2
                                • J
                                  John Doe 1 @Alan Kilborn
                                  last edited by Apr 6, 2022, 2:10 PM

                                  @alan-kilborn said in Adding Libraries to PythonScript Plugin:

                                  Well, I could as I am very familiar with Pandas…but I refuse to help with it here. :-)

                                  Understandable, if you can point me to a more specific forum for Pandas I’d appreciate it. Thanks for your help so far.

                                  A 1 Reply Last reply Apr 6, 2022, 3:16 PM Reply Quote 0
                                  • A
                                    Alan Kilborn @John Doe 1
                                    last edited by Apr 6, 2022, 3:16 PM

                                    @john-doe-1 said in Adding Libraries to PythonScript Plugin:

                                    point me to a more specific forum for Pandas

                                    Hmm, can’t say I’ve ever used one.
                                    I searched it up; apparently a “more important” pandas is a medical condition. And of course the cute bears…

                                    But I did find this one, no idea of its quality: https://community.developers.refinitiv.com/tags/pandas.html

                                    I’m sure some more intensive searching will provide others.

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