The code is like this:
os.execlp('python', 'python', 'child.py', #other args#) # this works
os.execlp('python', 'child.py', #other args#) # this doesn't work
I read this question: execlp() in python
But I’m still confused. The answer said:
The first argument is the program to execute (found on the PATH). The
rest are the sys.argv arguments to the program.
However, if I run: python child.py 1 2 3 and the sys.argv of this process would be ["child.py", "1", "2", "3"], where the python doesn’t exist. Then why should I add python as the second parameter of os.execlp?
When python is executed, it creates
sys.argvfor you. The values in that list are based on the arguments passed to it by the operating system, but it leaves off thesys.executablevalue from that list.In other words, when Python is invoked, it sets
sys.argvto everything but it’s own executable.When you invoke a new executable via
os.execlp(), you still need to include Python in that as that is what executable that the OS will run. The first two values of what you a pass toos.execlp()are still required, whatever you find insys.argvlater on.