Lea
Lea

Reputation: 221

How to find out from where is a Python script called?

I need to test whether several .py scripts (all part of a much bigger program) work after updating python. The only thing I have is their path. Is there any intelligent way how to find out from which other scripts are these called? Brute-forece grepping wasn't as good aas I expected.

Upvotes: 1

Views: 551

Answers (2)

Lea
Lea

Reputation: 221

I combined two things:

  • ran automated tests with the old and new version of pythona nd compared results
  • used snakefood to track the dependencies and ran the parent scripts

Thanks for the os.walk and os.getppid suggestion, however, I didn't want to write/use any additional code.

Upvotes: 0

jordibc
jordibc

Reputation: 56

Use os.getppid() to get the parent PID from the process, and then you can grep for it or similar.

For example:

import os
import subprocess

ppid = os.getppid()
output = subprocess.check_output(['ps', str(ppid)])

print 'Some info about my parent process (%d):' % ppid
print output.strip().split('\n')[-1]

Upvotes: 1

Related Questions