1

I created a test tool to check if there are any modules that do not work with pyinstaller so I can work them out before using pyinstaller on my main program.

When I try to interact with files paths in my script it looks like the program that pyinstaller created can not find the paths I have tried to hard code into the script such as "Z:\mkb\crew\mark_conrad\pictures\psd_tool_test_files\test.psd". I decided to use a simply os.path.exists() to debug this mystery but with no luck. When I run my debug program from python console it works just fine so what is going wrong here?

How I generated the exe: pyinstaller "Z:\mkb\programing\python\util\pyinstaller_library_tester.py"

Python version: 2.7.15 PyInstaller version: 3.3.1

Consul output:

Testing: Z:\mkb\crew\mark_conrad\pictures\psd_tool_test_files\test.psd >>> This path does not exsist. Path Results: False Testing: Z:\\mkb\\crew\\mark_conrad\\pictures\\psd_tool_test_files\\test.psd >>> This path does not exsist. Path Results: False Testing: Z:/mkb/crew/mark_conrad/pictures/psd_tool_test_files/test.psd >>> This path does not exsist. Path Results: False Testing: Z://mkb//crew//mark_conrad//pictures//psd_tool_test_files//test.psd >>> This path does not exsist. Path Results: False 

Debug program Code:

def checkingPaths(path,btn): import os if os.path.exists(path): print '>>> Found a working path use this for your formats for paths' print 'Path Results:',os.path.exists(path) btn.configure(bg='#00cc30') else: print '>>> This path does not exsist.' print 'Path Results:',os.path.exists(path) btn.configure(bg='#ff0000') def osTest(btn): print r'Testing: Z:\mkb\crew\mark_conrad\pictures\psd_tool_test_files\test.psd' checkingPaths("Z:\mkb\crew\mark_conrad\pictures\psd_tool_test_files\test.psd",btn) print r'Testing: Z:\\mkb\\crew\\mark_conrad\\pictures\\psd_tool_test_files\\test.psd' checkingPaths("Z:\\mkb\\crew\\mark_conrad\\pictures\\psd_tool_test_files\\test.psd",btn) print r'Testing: Z:/mkb/crew/mark_conrad/pictures/psd_tool_test_files/test.psd' checkingPaths("Z:/mkb/crew/mark_conrad/pictures/psd_tool_test_files/test.psd",btn) print r'Testing: Z://mkb//crew//mark_conrad//pictures//psd_tool_test_files//test.psd' checkingPaths("Z://mkb//crew//mark_conrad//pictures//psd_tool_test_files//test.psd",btn) def tkinterTest(): import Tkinter as tk root = tk.Tk() osBtn = tk.Button(root,text='os Test',command =lambda: osTest(osBtn)) osBtn.pack(padx=10,pady=2,fill='x') root.mainloop() tkinterTest() 

1 Answer 1

6

It creates a new path you have to use when "compiled" under sys._MEIPASS. I generally make a function that resolves the relative resource path depending on whether running in python or when "compiled", like so:

def get_correct_path(relative_path): try: base_path = sys._MEIPASS except Exception: base_path = os.path.abspath(".") return os.path.join(base_path, relative_path) 

Also ensure you properly include the files in your spec file.

Sign up to request clarification or add additional context in comments.

9 Comments

thanks you I am new to doing this with pyinstall. Is this a function I would need to add to my Debug program code and when ever I call a path through a function I would need to run it through get_correct_path Function to get the correct path?
@MarkC that's how I use it so I don't have to do this try/except every time I need a path. This way it "just works" whether running in python in dev or running compiled via pyinstaller in prod. Keep in mind you need to add all files to spec and they need to be in paths relative to code, so I usually put all files I need in the main source folder or a subfolder in project
So if I built a pyinstall script that generates folders or txt files I would need to add the path to those files and folders to the spec file that is generated with pyinstaller?
@MarkC no. If the app generates the files, so long as you know the absolute path they are written to you're fine. This is for files where you don't know the absolute path (files you add to the executable/installer) and so need to resolve them relative to the application binary
@MarkC if this helped you, please consider upvoting and/or marking this as the accepted answer.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.