Community
    • Login

    When Running Python from NPP, the Script Can't Find Local Image Files

    Scheduled Pinned Locked Moved General Discussion
    8 Posts 3 Posters 616 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.
    • Ron Tarrant 1R
      Ron Tarrant 1
      last edited by

      I’m using the NppExec plugin to run Python and I’ve used a few variations to get this working, none of which allow the script to find local images. Here are the variations I’ve tried:

      cmd /K cd "$(FULL_CURRENT_DIRECTORY)" & python "$(FULL_CURRENT_PATH)" & pause & exit
      
      cmd /k "$(FULL_CURRENT_PATH)"
      
      cmd /k c:\path\to\python.exe "$(FULL_CURRENT_PATH)"
      
      cmd /s /k "$(FULL_CURRENT_PATH)"
      

      And here’s an example script:

      from tkinter import *
      from PIL import Image
      #import sys
      #import os
      #
      ### get the execution path
      #path  =  os.path.abspath(os.path.dirname(sys.argv[0]))
      ##
      root  =  Tk()
      root.title("Game")
      
      frame  =  Frame(root)
      frame.pack()
      
      width = 700
      height = 400
      centre_x = width / 2
      centre_y = height / 2
      
      canvas  =  Canvas(frame, bg = "black", width = width, height = height)
      canvas.pack()
      
      
      #background  =  PhotoImage(file = path + "/images/for_loop_breakdown.png")
      background  =  PhotoImage(file = "images/for_loop_breakdown.png")
      canvas.create_image(centre_x, centre_y, image = background)
      
      #character  =  PhotoImage(file = path + "/images/rectangle.png")
      character  =  PhotoImage(file = "images/rectangle.png")
      canvas.create_image(centre_x, centre_y, image = character)
      
      root.mainloop()
      

      If I uncomment the lines defining the execution path as well as the lines below defining ‘background’ and ‘character’, everything works as expected. But as is, I get the following error:

      _tkinter.TclError: couldn't open "images/for_loop_breakdown.png": no such file or directory
      

      Can anyone point me in the right direction?
      Thanks.

      PeterJonesP Alan KilbornA 2 Replies Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @Ron Tarrant 1
        last edited by

        @Ron-Tarrant-1 ,

        you don’t need to do it all in a single cmd call … and the /K option says “keep this cmd shell running”, which means you’ve actually superceded the NppExec console and jumped to a persistent cmd.exe environment, so until you manually type exit from that shell, you’ll be stuck in that cmd.exe shell.

        What you really want, if you need to execute one command that doesn’t execute straight from the NppExec script itself, is to use cmd.exe /c, so that cmd will return control to NppExec when it’s done, rather than keeping the cmd shell open.

        So what you probably want is something more like

        npp_save
        cd "$(CURRENT_DIRECTORY)"
        cmd.exe /c python "$(FULL_CURRENT_PATH)"
        

        … and since you changed into the file’s directory already, you can actually use "$(FILE_NAME)" instead of "$(FULL_CURRENT_PATH)"

        Ron Tarrant 1R 1 Reply Last reply Reply Quote 0
        • Ron Tarrant 1R
          Ron Tarrant 1 @PeterJones
          last edited by

          @PeterJones said in When Running Python from NPP, the Script Can't Find Local Image Files:

          npp_save
          cd “$(CURRENT_DIRECTORY)”
          cmd.exe /c python “$(FULL_CURRENT_PATH)”

          Okay, I’m confused…

          I tried doing what you said… not putting everything on one line. But the Run dialog only has one line. So, I tried just spacing it out. $(CURRENT_DIRECTORY) and $(FILE_NAME) both expanded properly, but I got this error:

          2024-01-30 14_40_57_ShellExecute - ERROR.png

          What am I missing here? Did I misunderstand you?

          PeterJonesP 1 Reply Last reply Reply Quote 0
          • Alan KilbornA
            Alan Kilborn @Ron Tarrant 1
            last edited by Alan Kilborn

            @Ron-Tarrant-1:

            From this:

            I’m using the NppExec plugin to run…

            we were led to believe you were using NppExec. Now you’re changing it up:

            …But the Run dialog only has one line…

            So, Peter’s reply is in the form of an NppExec script, because that’s what you said you were using.


            Anyway, if you really want to use the Run dialog, you can put multiple commands in it, like this example:

            cmd /c cd /d c:\ && dir && pause

            which changes to the root directory on C:, creates a directory listing there, and pauses before closing (so you can see that it worked).

            Note that if you do go with the Run dialog approach, you’d have to manually save your file first, as it won’t know what npp_save means.

            1 Reply Last reply Reply Quote 1
            • PeterJonesP
              PeterJones @Ron Tarrant 1
              last edited by PeterJones

              @Ron-Tarrant-1 said in When Running Python from NPP, the Script Can't Find Local Image Files:

              But the Run dialog only has one line.

              In case Alan’s response wasn’t clear enough: if you have the NppExec plugin, you should use that for this task: trying to do it in the Run dialog will be fraught with difficulties.

              One of the main points of the NppExec plugin is to make something equivalent to a “batch” file language for N++, with lots of extra features compared to a true .bat file, which goes beyond the simple (and limiting) interface of the Run dialog.

              Since you said you have NppExec, then use it. If you don’t actually have it, then install it, because it will make this and other such activities much simpler.

              In general: if your Run > Run command involves any && or & or ||, or more than one $(...) variable, switch to NppExec

              Ron Tarrant 1R 2 Replies Last reply Reply Quote 3
              • Ron Tarrant 1R
                Ron Tarrant 1 @PeterJones
                last edited by

                @PeterJones Thank you for your kind response. My intention wasn’t to mislead. I’ve been labouring under the impression that nppExec was part and parcel of the Run dialog. Now I know better. I seriously don’t know how I got that impression, but there we are.

                And I appreciate you being considerate of my feelings in explaining things. I’ll go read up on nppExec now and hopefully get something working.

                Thank you.

                1 Reply Last reply Reply Quote 0
                • Ron Tarrant 1R
                  Ron Tarrant 1 @PeterJones
                  last edited by Ron Tarrant 1

                  @PeterJones
                  I’ve got a basic nppExec script working:

                  NPP_SAVE
                  cd "$(CURRENT_DIRECTORY)"
                  cmd.exe /c py "$(FILE_NAME)"
                  

                  But print() statements in a Python script don’t show up in the console until after the script exits. This isn’t obvious unless Python opens a GUI window.

                  Is there a way to have them show in real time as the script is executing?

                  Ron Tarrant 1R 1 Reply Last reply Reply Quote 0
                  • Ron Tarrant 1R
                    Ron Tarrant 1 @Ron Tarrant 1
                    last edited by

                    @Ron-Tarrant-1
                    It’s okay. I found the answer on Stackoverflow. In case someone else comes looking for this, here’s the entire script (note the ‘-u’ arg after ‘py’):

                    NPP_SAVE
                    cd "$(CURRENT_DIRECTORY)"
                    cmd.exe /c py -u "$(FILE_NAME)"
                    

                    Thanks again for all the help.

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