使用os.execlp时,为什么`python`需要`python`作为argv[0] [英] When using os.execlp, why `python` needs `python` as argv[0]

查看:15
本文介绍了使用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:

第一个参数是要执行的程序(在 PATH 中找到).这其余的是程序的 sys.argv 参数.

The first argument is the program to execute (found on the PATH). The rest are the sys.argv arguments to the program.

但是,如果我运行: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]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆