简体   繁体   中英

Running bash_profile commands with python

In my bash terminal, I can run the following command:

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

and this works correctly.

I am then trying to run the same command in a python script; where I have tried the following approach:

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

which throws the error:

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

I am confused what the issue could be, as I can run basic bash commands fine. Thank you for any help.

Environment variable expansion is not done by default using subprocess unless you pass the shell argument to the subprocess.run call. This will pass your arguments to a shell (eg bash ) which will handle the desired expansion and any other shell-like functionality.

Here's the code change necessary:

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

Note that it is worth reading through the security considerations for using this given in the python documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM