【问题标题】:Running bash_profile commands with python使用 python 运行 bash_profile 命令
【发布时间】:2019-01-17 11:23:13
【问题描述】:

在我的 bash 终端中,我可以运行以下命令:

$SCHRODINGER/run volume_calc.py -imae type.mae

这可以正常工作。

然后我尝试在 python 脚本中运行相同的命令;我尝试过以下方法:

import subprocess
subprocess.run(['$SCHRODINGER/run', 'volume_calc.py', '-imae', 'type.mae'])

引发错误:

FileNotFoundError: [Errno 2] No such file or directory: '$SCHRODINGER/run'

我很困惑问题可能是什么,因为我可以正常运行基本的 bash 命令。感谢您的帮助。

【问题讨论】:

    标签: python bash subprocess


    【解决方案1】:

    默认情况下不使用 subprocess 完成环境变量扩展,除非您将 shell 参数传递给 subprocess.run 调用。这会将您的参数传递给 shell(例如 bash),该 shell 将处理所需的扩展和任何其他类似 shell 的功能。

    这里是必要的代码更改:

    subprocess.run(['$SCHRODINGER/run', 'volume_calc.py', '-imae', 'type.mae'], shell=True)
    

    请注意,使用 python 文档中给出的 security considerations 是值得阅读的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      • 2014-07-18
      • 2017-12-11
      • 2013-01-31
      • 2020-11-28
      相关资源
      最近更新 更多