首页 > 解决方案 > 使用`time`命令通过shebang运行python脚本

问题描述

我有一个 python 脚本,我希望能够从 bash 运行它。
这通过shebang简单地解决了。
下一步是在 shebang 中实现 time 命令。
我最好但不是完全成功的想法是使用

#!/usr/bin/env -vS bash -c "time /usr/bin/python3 -OO"

遗憾的是,这并没有让 python 解释脚本文件并以交互式 python 会话结束。

输出是

split -S:  ‘bash -c "time /usr/bin/python3 -OO"’
 into:    ‘bash’
     &    ‘-c’
     &    ‘time /usr/bin/python3 -OO’
executing: bash
   arg[0]= ‘bash’
   arg[1]= ‘-c’
   arg[2]= ‘time /usr/bin/python3 -OO’
   arg[3]= ‘./mypycheck.py’
Python 3.7.3 (default, Apr  3 2019, 05:39:12)

我怎样才能完成这项工作?提前致谢。

标签: pythonbashtimeshebang

解决方案


At the end summing up all helpful details from here, I was able to reach my goal with the following solution.

  1. Installing time utiliy by running sudo apt install time
  2. Using the shebang #!/usr/bin/env -S /usr/bin/time /usr/bin/python3 -OO

And now all is running the way I was looking for.


推荐阅读