Change Python Process Name – Best Way To Figure Out The Python You Want

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.

Change python process name
Change python process name

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:

python-by-name.bat
 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?

 set PYTHON_HOME=%~dp0

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.

 set PYTHON_NAME=%1.exe

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 PYTHON_NAME

 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.

2 thoughts on “Change Python Process Name – Best Way To Figure Out The Python You Want

  1. I have a similar problem with batch files. It is so frustrating.

    I am running several batch files. How can I change the process name of a batch file? When you open the Windows Task Manager, I only cmd.exe process. Therefore, I believe that the cmd.exe command runs the batch files.

    Can you adjust your script to allow me to find the batch script I want?

Leave a Reply

Your email address will not be published. Required fields are marked *