Anyway to see what project is being exeuted by python?

I’m trying to find a way with psutil to see the files being processed by python and I’ve found nothing. I’m running a bot program and I have code in place to Git pull from repository to update code inside the bot and then restart the bot but sometimes I get the message saying that git was unable to unlink a file. This is due to a lock on the file 'cause it is still being processed by python. I need to be able to see which python executable what is executing the bot and kill it.

So you know I have in place os.startfile that is executing another script that of course waits for the bot to close and then does the git pull update and vise, after the get update is done the script relaunches the bot and closes itself.

Without using a debugger and know the internals of python its hard to know what python modules where load.

But since your real question is about why there is a lock on a file why not ask for help with that issue?

What is the message you see about a lock?
Also what version of python and on what OS is this running?

Sounds like your problem isn’t really Python-specific, it’s the broader question “which program is holding this file open”. There should be solutions out there, although personally I’m not familiar with them, as I use a file system that doesn’t prevent the unlinking of files while they’re open.

3.11.8 on Windows 11 Pro

And what Chris Angelico said is what my problem is.
Sounds like your problem isn't really Python-specific, it's the broader question "which program is holding this file open". except that it is python that is holding my bot project open.

I have several projects running and when I look in the task manager what all see is py.exe, pyw.exe, python.exe, and pythonw.exe and there isn’t a drop down arrow for any of them.

I do know that py.exe, and pyw.exe are the parent programs to both python.exe, and pythonw.exe.

And to be specific the git error is: unable to unlink old 'schema/commands.py': Invalid argument

That not an error about locking.
Sounds like you got the path to a file wrong or you are in the wrong current directory.

It does appear to be about the file being locked, but either git-on-windows or the windows API suck at communicating that. (I would have expected “permission denied”)

The best way to distinguish one Python process from another is probably the command-line arguments. I don’t know enough about Windows to say how you’d do that. But if you know exactly which process is holding the file open, you would know which one needs to be shut down.

I figured out the source of my problem.

module = open('schema/commands.py')
Commands = [function.name for function in parse(module.read()).body
            if isinstance(function, FunctionDef) and function.name not in [
                '_globals',
                'download',
                'debug',
                'crash',
                'message',
            ]]

Opened it but never closed it.

Replaced it with

with open('schema/commands.py') as module:
    Commands = [function.name for function in parse(module.read()).body
                if isinstance(function, FunctionDef) and function.name not in [
                    '_globals',
                    'download',
                    'debug',
                    'crash',
                    'message',
                ]]

Finding this bug fixed my entire problem and I was able to put all of the code for the git pull update back into the bot.

The link you provided help me find the source of my problem. The only file that was showing up in Resource Manager->CPU->Associated Handles out of all the files in the my project was the commands.py. The only file that was causing the “unable to unlink file.”

1 Like