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

Asked

Viewed 50 times

0

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

The problem is precisely in using the executable for Mac OS done with pyinstaller. When I click on such executable, it understands that os.getcdw() is precisely the user folder. It does not take the folder where the executable file is.

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

he returns "/Users/isaacvictor"

How do I resolve this issue? I want to grab the executable file folder.

Many thanks to all who contribute.

  • The os.getcwd() returns the current working directory, if you want to use another you can open, something like: meu_dir = os.open( "/Users/isaacvictor/Desktop/", os.O_RDONLY ), and then set it as your standard working dir : os.fchdir(meu_dir), when you call the os.getcwd() will work.

  • @Interesting tuxpilgrim, what’s the difference of opening a fd pro directory, than simply use os.chdir("/Users/isaacvictor/Desktop/") directly? I think it’s the same thing, it’s much more complex.

  • So @nosklo suggested this option because it was the first one I thought about. As for your question, taking a look at doc (in a Linux scenario)we have that the two are virtually identical except for the fact that the os.fchdir() gets a fd. This implies that not only directories can be passed by parameter, since in Linux everything is treated as a file, so sockets and devices can be opened and your fd recovered, thus giving to carry out operations on them. So for this case the os.chdir() is the best solution ;)

  • Complementing: from the version 3.3 python’s os.chdir() also supports the file Descriptor as input.

1 answer

1


As you can see in the documentation here, during the implementation of a programme frozen with pyinstaller the path of the executable is in sys.executable - the problem is that it does not happen when you run the script in format .py, only on executable;

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

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 how the program is running, before or after the pyinstaller.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.