抱歉,这是一个非常新的问题,但我似乎无法让它发挥作用。
在我的 bash 脚本中,我有
python=/path/to/python
script=$1
exec $python $script "$@"
我如何传递一个参数,比如 -O 给 python 解释器?我试过:
执行 $python -O $script "$@"
并尝试将 python 变量更改为“/path/to/python -O”,以及将 -O 传递给脚本,但每次我执行这三个中的任何一个时,我都会遇到成功模块的导入错误我删除了 -O。
所以我的问题是如何告诉 python 解释器使用 bash 脚本中的 -O 参数运行?
谢谢。
最佳答案
您应该将您的位置参数向左移动 1,以排除第一个参数中的脚本被包含在 python 的参数中。
#!/bin/sh
python=/path/to/python
script=$1; shift
exec "$python" -O "$script" "$@"
然后将脚本作为 bash script.sh your_python_script arg1 arg2 ...
或 sh script.sh your_python_script arg1 arg2 ...
运行。
关于python - 从 bash 脚本向 python 解释器传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18562956/