Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Pyinstaller on mac current directory problem

I have created a little script that I want to run as an executable on Mac and Windows.

I created the executable as one file using --onefile and I want to work with files in the same directory as the executable.

In windows os.getcwd() works fine after using pyinstaller but on mac it reverts to the base path:

> /Users/User
Traceback (most recent call last):
  File "test.py", line 93, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '/Users/user/Invoices/'
[62121] Failed to execute script test
logout

When I run it as a .py file however it gets the right directory on mac as well.

I have tried changing os.getcwd() to os.path.realpath(__file__) yet it still gives the wrong path when converted with pyinstaller.

I want to be able to move the executable around on mac and work with whatever directory it is in.

like image 400
Dutchpirate91 Avatar asked Mar 20 '26 03:03

Dutchpirate91


1 Answers

It turns out that the following works:

dir_path = slash.join(sys.argv[0].split(slash)[:-1])

this works only when using the executable on mac. On windows I still use os.getcwd and when running the python script as well.

like image 98
Dutchpirate91 Avatar answered Mar 22 '26 17:03

Dutchpirate91



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!