pythonnotepad++relative-path

Get Notepad++ to use relative path to image


I can open any python file from Notepad++ using F5, unless it refers to an image with a relative path. There must be a way to make Notepad++ F5 work the same as double-clicking the file name in Windows Explorer or right-clicking the same file name to "Edit with IDLE". How do I get Notepad++ to open a file without hard-coding into my .py file the full path as it exists on my computer? The end user of my app doesn't need the path that's on my computer. I couldn't find instructions on how a beginner like me could fix NPP to do this right. I'm using NPP v7.4.2 32-bit on Windows 7. I've tried the NPP forum but neither its search engine nor Google have turned up an answer.

F5 opens the file correctly only if I hard-code the full path as shown in the top uncommented line of code.

I thought I'd found the answer here but see the second error message below when I tried

img4 = tk.PhotoImage(file=os.path.abspath("joe.gif") , master=root)

Apparently NPP is forcing an absolute path on me.

Thanks for any assistance.

import tkinter as tk

root = tk.Tk()

# img4 = tk.PhotoImage(file="joe.gif", master=root)
img4 = tk.PhotoImage(file="c:/tkinter_code/joe.gif" , master=root)
logoimage = tk.Label(root, image=img4)
logoimage.image = img4
logoimage.grid()

root.mainloop()


Traceback (most recent call last):
File "C:\tkinter_code\how_to_get_npp_to_display_images_with_relative_path_tkinter.py", line 9, in <module>     img4 = tk.PhotoImage(file="joe.gif", master=root) File "C:\Users\LUTHER\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 3393, in __init__Image.__init__(self, 'photo', name, cnf, master, **kw) File "C:\Users\LUTHER\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__ init__.py", line 3349, in __init__ self.tk.call(('image', 'create', imgtype, name,) + options) _tkinter.TclError: couldn't open "joe.gif": no such file or directory

Traceback (most recent call last):
File "C:\tkinter_code\how_to_get_npp_to_display_images_with_relative_path_tkinter.py", line 14, in <module>     img4 = tk.PhotoImage(file=os.path.abspath("joe.gif") , master=root) File "C:\Users\LUTHER\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 3393, in __init__    Image.__init__(self, 'photo', name, cnf, master, **kw) File "C:\Users\LUTHER\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 3349, in __init__    self.tk.call(('image', 'create', imgtype, name,) + options) _tkinter.TclError: couldn't open "C:\Program Files\Notepad++\joe.gif": no such file or directory

Solution

  • You'll need to have NPP run the script with the working directory set correctly, i.e. to the path of your file.

    There's an answer here for it: simply change the command being run to

    cmd /K cd "$(CURRENT_DIRECTORY)" && python "$(FULL_CURRENT_PATH)"