0

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
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Luther
  • 514
  • 4
  • 17
  • It's not clear what you're asking. Are you asking how to open a file in notepad++, or how to open a file in a python/tkinter script? Are you aware that relative paths are relative to the _current working directory_ which may not always be the same as the directory where the script is located? – Bryan Oakley Dec 29 '18 at 16:23
  • Ok, so this has absolutely nothing to with tkinter. – Bryan Oakley Dec 30 '18 at 02:58
  • Right, but this problem can be duplicated with a simple `open` statement. Tkinter is completely unrelated. – Bryan Oakley Dec 30 '18 at 05:24

2 Answers2

2

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)" 
AKX
  • 152,115
  • 15
  • 115
  • 172
  • 1
    I do believe this is the answer you're actually looking for, though. Judging from the traceback, your program is being run with "C:\Program Files\Notepad++" as the working directory; the command in the answer will change the working directory before starting the script. Your answer changes the working directory to a hard-coded one from within the script. – AKX Dec 29 '18 at 12:27
  • You're right. My answer would have forced me to import os to every file that has images whereas your answer fixed my NPP F5 command and this has nothing to do with the end user nor does it hard code anything into my file except the relative path. That's the answer I was looking for. Thanks. – Luther Dec 29 '18 at 12:59
0

Here's the answer. To do it all in Python so I don't have to change NPP:

import os
os.chdir("C:/tkinter_code")

By adding a Python direction the file now opens correctly from NPP F5, image and all.

Luther
  • 514
  • 4
  • 17
  • But now your program and image can't be moved to another directory without it breaking. – AKX Dec 29 '18 at 12:25