本文介绍了使用os.execlp时,为什么`python`需要`python`作为argv [0]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码如下:

os.execlp('python', 'python', 'child.py', #other args#)  # this works

os.execlp('python', 'child.py', #other args#)  # this doesn't work

我阅读了以下问题: python中的execlp()

但是我还是很困惑.答案说:

But I'm still confused. The answer said:

但是,如果我运行:python child.py 1 2 3,则此过程的sys.argv将是["child.py", "1", "2", "3"],而python不存在.那为什么要添加python作为os.execlp的第二个参数?

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?

推荐答案

执行python时,它将为您创建sys.argv.该列表中的值基于操作系统传递给它的参数 ,但是它从该列表中删除sys.executable值.

When python is executed, it creates sys.argv for you. The values in that list are based on the arguments passed to it by the operating system, but it leaves off the sys.executable value from that list.

换句话说,当调用Python时,它会将sys.argv设置为所有但它是自己的可执行文件..

In other words, when Python is invoked, it sets sys.argv to everything but it's own executable.

当通过os.execlp()调用 new 可执行文件时,您仍然需要包含Python,因为这是操作系统将运行的可执行文件.传递给os.execlp()的内容的前两个值仍然是必需的,无论您稍后在sys.argv中找到什么.

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 to os.execlp() are still required, whatever you find in sys.argv later on.

这篇关于使用os.execlp时,为什么`python`需要`python`作为argv [0]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 11:19