[Python] Why is my Python logger placing log files in a completely different place from where...

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    (Edit: skip to the end, this is all just context. I found the core of the issue later)

    I'm writing a Python UI program that I aim to compile to a .exe with pyinstaller, and for that reason I wish to configure the logging module to point the root logger to a FileHandler pointing to a user logs folder fetched from the platformdirs module. This is what my logging setup function looks like, which I run very soon in my program's execution:

    def setup_logging():
    global LOGS_FOLDER

    """If we're running inside an .exe file, setup log files in AppData"""
    if hasattr(sys, "_MEIPASS"):
    LOGS_FOLDER = Path(user_log_dir(globals.APP_NAME))
    LOGS_FOLDER.mkdir(parents=True, exist_ok=True)

    log_file = LOGS_FOLDER / globals.LOG_FILE_NAME
    log_file_handle = open(log_file, "a", encoding="utf-8")

    console_handler = logging.StreamHandler(sys.stdout)
    file_handler = logging.FileHandler(log_file, encoding='utf-8')

    logging.basicConfig(level=logging.DEBUG, handlers=(console_handler, file_handler), encoding='utf-8', force=True)

    sys.stdout = log_file_handle

    else:
    logging.basicConfig(level=logging.DEBUG)


    In development, NOT as an .exe file, all I'm doing is running the main.py function in the command line. In both contexts (development main.py file and .exe file), the platformdirs log directory is pointing to C:\Users\[me]\AppData\Local\[app name]\[app name]\Logs, as I expect. However, only in the development run, for some reason these files are being placed in a completely different location: C:\Users\[me]\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\Local\[app name]\[app name]\Logs.

    What's even stranger is that when I do compile my program with pyinstaller and make it all a .exe file, this problem is gone, and the files are placed in the first path I provided.

    One more important detail: To test this code in development, at the moment all I'm doing is putting a not in front of hasattr so that the FileHandler code is run in development. Not a very permanent solution, but I expect this to behave the same as when it's an .exe file without the not there, and it does not, which makes me uncomfortable and I want to know what's happening.

    Does someone know what could be happening? Thank you. I've been using Visual Studio Code, but I just ran python3 main.py in a straight command prompt and this is still happening, so it can't be my IDE.

    Update: I tried to build up the target path until I found this behavior. It obeyed being put in C:/Users/ and C:/Users/[me]/, but the moment I tried to make it put the file in C:/Users/[me]/AppData it changed the directory to C:\Users\[me]\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache. Due to this, I opened a file in the most traditional with open... as f:, and tried to place it in my user AppData and write something, and it created the file in the strange LocalCache directory again. So my issue has nothing to do with logging, and Python is for some reason redirecting AppData to that other location.

    Continue reading...

Compartilhe esta Página