When Running Python from NPP, the Script Can't Find Local Image Files
-
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. -
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 persistentcmd.exe
environment, so until you manually typeexit
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)"
-
@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:
What am I missing here? Did I misunderstand you?
-
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. -
@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 -
@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.
-
@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-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.