vba kill python script

Vbalearner85

Board Regular
Joined
Jun 9, 2019
Messages
139
Office Version
  1. 2016
Platform
  1. Windows
Hi I am using below vba to run python script "Data.py" successfully and also run some others scripts "data1.py"etc parallelly via vba.

I am looking for vba which shall only kill/exit "Data.py" script and shall not kill/let other scripts keep running.

VBA Code:
Sub PyData()

ChDir "C:\Users\Desktop\Latest"
Call Shell("C:\Users\AppData\Local\Programs\Python\Python39\python.exe " & "Data.py", 1)

End Sub

I am aware of kill method to exit python completly but looking for vba which shall only kill particular python script as mentioned above. Please advise

VBA Code:
Sub KillPython()

Dim sKillExcel As String

sKillExcel = "TASKKILL /F /IM python.exe"
Shell sKillExcel, vbHide

End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hi. So you need to find out what's called the Process ID for your execution of that specific script. You can get this when you execute the program, and then you use that Process ID with the taskkill command you have above. So:

VBA Code:
Dim ProcessID As Long
Sub PyData()

ChDir "C:\Users\Desktop\Latest"
ProcessID = Shell("C:\Users\AppData\Local\Programs\Python\Python39\python.exe " & "Data.py", 1)

End Sub
Sub KillPython()

Shell "TaskKill /F /PID " & ProcessID, vbHide

End Sub
If you're going to execute and kill the process in separate subroutines, as you have in your question, then you need to decide how you're going to store the ProcessID so you can call it when you want to terminate the process. In my example above, I have made the variable accessible to both routines, but this may not be appropriate for your specific purposes.

But let me know if that works, or if you need any other help.
 
Upvote 0
Thanks for the proposed solution.....I researched a little more and have adopted a slightly different approach - mix of python and vba

1) getting PID from python script itself and write in excel (which script is acting upon)
2) Use that PID from excel in vba to kill the particular script

VBA Code:
Sub Killpya()

ProcessID = ThisWorkbook.Sheets("Control").Range("L7").Value2
Shell "TaskKill /F /PID " & ProcessID, vbHide

End Sub
 
Upvote 0
Ok - glad you managed to arrive at a solution. For the benefit of one else who might have a similar issue (but most importantly, me!!! :-) ) - and while I appreciate this falls on the python side of the hybrid solution - how did you: (1) obtain the Process ID; and (2) insert that value into Excel (cell L7), in Python?
 
Upvote 0
I have modified my python script to include below code

# Python program to explain os.getpid() method

# importing os module
import os

# Get the process ID of
# the current process
pid = os.getpid()


# Print the process ID of
# the current process
print(pid)

#Define wb and write pid in excel
wb = xw.Book('Data.xlsm')
sht2 = wb.sheets['Control']
sht2['L7'].value = pid


Thanks
 
Upvote 0
Great, thank you. The variables you've used look like you've used the XLWINGS library.
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top