gpt4 book ai didi

python - Bash 脚本到 python 脚本的转换

转载 作者:行者123 更新时间:2023-12-01 09:30:42 36 4
gpt4 key购买 nike

BASH 代码:

source /proj/common/tools/repo/etc/profile.d/repo.sh
repo project init $branch
repo project sync
source poky/fnc-init-build-env build
bitbake -g $image

我将此 bash 代码转换为 python(版本 2.7)。在执行 python 代码时,我收到 repo command not found 消息。

Python 代码:

os.system("source /proj/common/tools/repo/etc/profile.d/repo.sh")
os.system("repo project init " + branch)
os.system("repo project sync")
os.system("source poky/fnc-init-build-env build")
os.chdir("poky/build")
os.system("bitbake -g " + image)

错误消息:

sh: repo: command not found
sh: repo: command not found

我尝试使用 subprocess.call(),但收到了相同的错误消息。

最佳答案

问题出在这个调用中:

os.system("source /proj/common/tools/repo/etc/profile.d/repo.sh")

问题是它在单独的子 shell 中运行 source ,并且当子 shell 退出时对环境的所有更改(如果有任何环境变量,则使用 cd 命令,大多数值得注意的PATH)消失了。

我的建议是继续使用您已经使用过的 shell 脚本 — 只需通过一次 os.system() 调用从 Python 中调用它即可。在 shell 脚本中,您可以使用 source

关于python - Bash 脚本到 python 脚本的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49993364/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com