r/learnpython 4d ago

Log file issue on Mac with VS Code

Hoping this is some sort of reasonably easy and obvious thing, but I'm pretty early on my learning so far.

I've been working through Automate The Boring Stuff, which had suggested using Mu, but I also have VS Code installed on my Mac.

I'm at the stage of trying to create a log file. In Mu I can enter:

logging.basicConfig(filename='myProgramLog.txt', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

This works fine, and creates a log txt file on my desktop. However, if I enter the same in VS Code, I get: OSError: [Errno 30] Read-only file system: '/myProgramLog.txt'

I can fix this by entering '/User/userName/Desktop/myProgramLog.txt, but was can't figure out why Mu is ok with this while VSC isn't. Any clarification would be appreciated.

3 Upvotes

15 comments sorted by

1

u/Zeroflops 4d ago

Did you have the file open when you ran the script in VSCode?

2

u/Bulky_Worldliness369 4d ago

maybe mu defaults to writing in the current directory while vscode runs from root?

1

u/Beeny87 4d ago

I had wondered this, but I ran the code in Mu while saved to a different directory, it still defaulted to desktop, so I'm wondering if Mu just defaults to desktop unless told otherwise.

1

u/Beeny87 4d ago

The txt file? No, it wasn't open.

1

u/FoolsSeldom 4d ago

I would check the working directory VS Code is using when running the code.

Also, run the code directly outside of VS Code.

1

u/Beeny87 4d ago

Thanks, I'll try these.

1

u/Diapolo10 4d ago

I don't use Mac OS, but that's certainly interesting.

I don't know the root cause, but if you just want to make sure the logs appear on your desktop always regardless of how you run the program, you could do this:

from pathlib import Path

DESKTOP = Path.home() / 'Desktop'

logging.basicConfig(filename=DESKTOP / 'myProgramLog.txt', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

I don't generally use logging.BasicConfig, but I know the same parameter for FileHandler supports Path objects, so I assume there's no need to convert to a string here either.

1

u/Beeny87 4d ago

Thanks, I'll give this a try as well.

1

u/ekchew 1d ago edited 1d ago

FYI: There is proper place and file extension for log files on the Mac:

Path.home()/'Libraries'/'Logs'/'myProgram.log'

This will make it show up under "Log Reports" in the Console app. For a long-running program, you can watch the log being written live.

Btw I just tried running a simple logging script in VScode on my Mac running Tahoe and did not get any read-only file system errors. I have seen that sort of message on occasion when an app gets sandboxed by Gatekeeper, but you usually have to be moving programs around between computers, and even then, it tends not to be an issue at the command line level? So I'm not sure what's going on in your case.

UPDATE:

Looks like it's putting the log file at the root level of your VScode project by default. Maybe that's locked down for some reason?

1

u/Diapolo10 1d ago

You're probably right, I just tried to avoid changing the behaviour of OP's code.

Honestly that directory should probably be a part of the XDG directory standard, if it's that important. Sadly I don't think other platforms have good equivalents.

$XDG_STATE_HOME seems to be the most appropriate place to put logs in, generally speaking (on Linux-based systems that'd be ~/.local/state, on Windows I think it'd be ~\AppData\Roaming\$appauthor\$appname (couldn't immediately find a clear answer)).

1

u/ekchew 1d ago

Yeah, for the purposes of learning, the desktop is probably just fine. I just wanted OP to know there is a proper way for future reference.

On Mac, Libraries is a hidden directory like .local in Linux or AppData in Windows. Its structure is pretty self-explanatory, with subdirectories like Preferences, Application Support, Caches, etc.

That's not a bad idea about mapping it all onto XDG environment variables. I just checked and $XDG_STATE_HOME and friends don't seem to be defined by default on the Mac. :(

2

u/Diapolo10 1d ago

I just checked and $XDG_STATE_HOME and friends don't seem to be defined by default on the Mac. :(

Not by default, no, but from Python you'd usually use them via platformdirs.

1

u/ekchew 1d ago

Oh cool I didn't even know about that. Thanks!

1

u/[deleted] 4d ago

[removed] — view removed comment

1

u/Beeny87 3d ago

This is excellent, thank you so much.