astorcas
astorcas

Reputation: 281

os.mkdir on Mac OS with PyInstaller built apps

I have the following code (I removed the watermark_text function because I know it does its job). The code works fine when running on Terminal as a .py script but if I build an executable with pyinstaller (on Mac OS) it keeps working if I run it from terminal but when I double-click it from finder something goes wrong and no exception is shown. If the folders DIR and OUD_DIR don't exist the os.mkdir function won't create them and if the folders exist os.listdir won't find any file. What's the issue here? I tried pyinstaller on Windows on the same code and it works perfectly. What am I missing?

DIR: Final[str] = "Images/"
OUT_DIR: Final[str] = "WMImages/"
LOGO: Final[str] = "logo.png"
 
if not os.path.exists(DIR):
    os.mkdir(DIR)

if not os.path.exists(OUT_DIR):
    os.mkdir(OUT_DIR)

image_list = os.listdir(DIR)
wm_image_list = os.listdir(OUT_DIR)

for img in image_list:
    image_file = Image.open(DIR + img)
    new_image = watermark_text(image_file)
    new_image.save(OUT_DIR + img)

EDIT:I figured it out by myself. The app works differently depending on it being run eiher as a script or an executable. It resolves app path differently and a workaround is to use absolute path finding the root using some code like this:

if getattr(sys, 'frozen', False):
    application_path = os.path.dirname(sys.executable)
else:
    application_path = os.path.dirname(__file__)

Upvotes: 0

Views: 19

Answers (0)

Related Questions