Figure Out the Python Process you want in Windows Task Manager is not an easy task. In this tutorial, we see how to overcome this issue and change python process Name to a unique one.
Figure Out The Python Process You Want In Windows Task Manager
You probably recognize the following frustrating situation: You invoke several python scripts or invoke the same python script several times. After some time, you decide you want to kill one of the python scripts you run. You believe it is an easy task – As always, you open Windows Task Manager, but you do not see the python script names but several
python.exe processes. Which one is the script you want to kill? How to figure out which python process is which? You do not have a clue.
Solution To Figure Out Which Python Process Is Which
Windows Task Manager displays the image name of the process. That is why Windows Task Manager shows python.exe for each python script we invoke regardless of the script name. However, If we could change the image name of the python.exe to a unique name for each python script file you want to run, we would quickly figure out which Python process Is want in Windows Task Manager. This is the idea behind the following solution:
Copy the python.exe to a unique name (For example unique.exe) for each python script you want to invoke. Next, invoke the unique.exe instead of python.exe. Since this unique.exe file is a copy of python.exe, we can invoke it as usual to execute any python script. However, When we open Windows Task Manager, we can easily figure out which python process is which since we will see several processes of different exe files instead of several processed of python.exe
Script To Change The Python Process Name
The following batch script automates the above idea. Create a new file named
python-by-name.bat in the python directory with the following contents:
set PYTHON_HOME=%~dp0 set PYTHON_NAME=%1.exe copy "%PYTHON_HOME%python.exe" "%PYTHON_HOME%%PYTHON_NAME%" set args=%* set args=%args:* =% "%PYTHON_HOME%%PYTHON_NAME%" %args%
What the script do?
First we find the python home directory. The
%~dp0 is the directory of the batch script we invoked. Since the file is located reside alongside the python.exe – It is Home of the python installation.
The copy’s name of of python.exe is the first argument to the script.
copy "%PYTHON_HOME%python.exe" "%PYTHON_HOME%%PYTHON_NAME%"
We copy python.exe to the
set args=%* set args=%args:* =% "%PYTHON_HOME%%PYTHON_NAME%" %args%
Now, we invoke the copy of the python.exe with the rest of the given arguments of the script.
How To Use?
Now, Let’s see how we can use this above batch to distinguish between your python scripts.
Suppose you want to run the following command:
Command A – Invoke with python
python my-script.py arg1 arg2 arg3 arg4
Invoke the following command instead:
Command B – Invoke with python-by-name
python-by-name my-script my-script.py arg1 arg2 arg3 arg4
The above commands : command A (python) and command B (python-by-name), will run the same script with the same arguments. However, when we open Windows Task Manager, we see that the item of command A under the image name process is python.exe while the item of command B under the image name process is my-script.exe.
If we change the unique name in command B for each python script we invoke, we can easily and quickly figure Out which python process you want in the Windows Task Manager.