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()
Mark C
  • 13
  • 1
  • 4

1 Answers1

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.

Arthur Dent
  • 1,828
  • 13
  • 21
  • 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? – Mark C May 12 '18 at 05:06
  • @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 – Arthur Dent May 12 '18 at 05:08
  • 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? – Mark C May 12 '18 at 05:39
  • @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 – Arthur Dent May 12 '18 at 19:59
  • @MarkC if this helped you, please consider upvoting and/or marking this as the accepted answer. – Arthur Dent May 14 '18 at 00:25
  • @ArthorDent It looks like this works just fine when looking for files / paths on my C:\ drive but when I try to do this with finding things on my Z:\ drive os.path.exists(path) returns with 'False' why is this? – Mark C May 14 '18 at 18:37
  • @MarkC no offense, but you haven't so much as upvoted this. I've answered the question. You now have new questions / are asking for ongoing tech support now. – Arthur Dent May 14 '18 at 18:54
  • NVM I had to use the absolute path for this to work no the 'Z:' drive. For other people if you are trying to get to a drive letter use the server name instead so for me Z:\mkb is '//Your-Server/server-folder/mkb' – Mark C May 14 '18 at 19:02
  • @MarkC yes, you will want to use the UNC path. See [this related answer](https://stackoverflow.com/a/45102663/9760446), but note if you do this your code will only work on Windows. – Arthur Dent May 14 '18 at 19:19