Problem with pyinstaller + os.getcwd () on MAC OS

0

After numerous problems with Tkinter and Pyinstaller, I was finally able to make a Unix executable application work normally on the MAC High Sierra. But inside the folder of my application, there is a folder called script, which I use chdir(os.getcdw()+'/scritp') to open it.

The problem is precisely in the use of the Mac OS executable made with pyinstaller. When I click on such an executable, it understands that os.getcdw() is just the user's folder. It does not take the folder where the executable file is.

For example, instead of os.getcdw() is equal to "/Users/isaacvictor/Desktop/scriexe"

it returns "/Users/isaacvictor"

How do I solve this problem? I want to get the executable file folder.

Many thanks to all who contribute.

    
asked by anonymous 13.08.2018 / 18:59

1 answer

1

As you can see in the documentation here , while running a frozen program with pyinstaller path of the executable is in sys.executable - the problem is that this does not happen when you run the script in .py format, only in the executable;

But there is a solution, you can check the special variable sys.frozen to see whether or not the executable is running:

import os
import sys

def pega_caminho_script():
    if getattr(sys, 'frozen', False) :
        # rodando executavel
        caminho = os.path.abspath(sys.executable)
    else:
        # rodando py
        caminho = os.path.abspath(__file__)
    return caminho  

This function will return the path to the .py or .exe file, depending on the form the program is running, before or after pyinstaller .

    
13.08.2018 / 20:18