Home Pyinstaller Packaged Application Execution Location in MacOS(English)
Post
Cancel

Pyinstaller Packaged Application Execution Location in MacOS(English)

Application Execution Location

When we use PyInstaller to package our Python program into an executable, the behavior for reading and writing files can differ between Windows and macOS. As shown in the picture below, we have an executable and a file.

Desktop View

The executable is a simple file-reading program:

1
2
with open("myfile.txt", "a", encoding="utf-8") as file: 
    file.write("I am here! \n")

On Windows, it works fine regardless of which folder you put them in.

However, on macOS, the file cannot be found, as if the application and your file are in different folders.

This is because on macOS, when you click on the executable packaged with PyInstaller, the current working directory (CWD) of the executable is usually not the directory where the executable itself is located. This is because the application runs in an environment called a “sandbox.”

Purpose of the Sandbox Mechanism

he sandbox on macOS is a security mechanism designed to restrict an application’s access to the system and user data, thereby enhancing the overall security and stability of the system. Here are some details about the macOS sandbox:

Isolation

The sandbox isolates each application, restricting its access to other applications, system resources, and user data. This prevents vulnerabilities in one application from affecting other parts of the system.

Security

By limiting the scope of an application’s access, the sandbox reduces the potential harm of malicious software. For example, an application running within the sandbox cannot access sensitive system areas or data from other applications, even if exploited by an attacker.

Privacy

The sandbox mechanism protects user privacy by ensuring that applications can only access explicitly authorized data. For instance, an application needs to request permission to access a user’s photos or contacts.

Many people have experienced that macOS devices are less prone to viruses, which is largely due to the sandbox mechanism significantly enhancing system security and making the system less vulnerable to viruses.

Solution

How to solve the issue of the executable and file being in different locations on macOS? The solution is simple: just include the current executable’s location in your program!

Here is the complete code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import os
import sys

def get_resource_path(relative_path):
    """Get the absolute path to a resource file."""
    if getattr(sys, 'frozen', False):
        # If the program is packaged
        base_path = os.path.dirname(sys.executable)
    else:
        # If the program is a regular Python script
        base_path = os.path.dirname(os.path.abspath(__file__))

    return os.path.join(base_path, relative_path)

with open(get_resource_path("myfile.txt"), "a", encoding="utf-8") as file:  
    file.write("I am here! \n")

Now, the packaged executable can correctly read and write files on both Windows and macOS.

That’s all.

☝ツ☝

This post is licensed under CC BY 4.0 by the author.

👈 ツ 👍