Bash 命令在终端中工作,但在 python 脚本的"子进程"中不起作用



我正在尝试使用一些粒子物理软件,并在每次变量略有变化的循环中运行它。它具有特定的功能,可以获取一个txt文件并扫描它以查找相关命令,所以我自然会尝试利用它。我已经写了一个python脚本(最初是用python3编写的,以便在我的笔记本电脑上运行,但我需要转到大学的集群。我已经转换为python2,因为粒子物理软件本身就是python 2,我不知道如何在集群上同时运行两个版本的python。具体来说,集群安装了python2.6.9,我没有任何能力更改它,我也无法更新它或安装新模块。我的代码中有问题的部分是:

def run_madgraph():
# starts madgraph and tells it to run mg5_aMC using mg_run_iridis.txt as a file for it.
print('starting....')
subprocess.check_call(['python', './madgraph/bin/mg5_aMC mg_run_iridis.txt'])

mg5_aMC启动物理软件,mg_run_iridis.txt是包含变量信息的文本文件。我不明白为什么这不起作用,因为以下命令在bash终端(集群上(中运行得很好:

python ./madgraph/bin/mg5_aMC mg_run_iridis.txt

据我所知,这个问题是在python bash方面,而不是物理软件中的任何问题。我得到的错误是:

(MGenv) [cb27g11@green0155 Development]$ python MainIridis_script.py 
about to run madgraph 1.0 0.9
starting....
python: can't open file '/home/cb27g11/Development/./madgraph/bin/mg5_aMC mg_run_iridis.txt': [Errno 2] No such file or directory
Traceback (most recent call last):
File "/home/cb27g11/Development/MainIridis_script.py", line 41, in <module>
the_main_event()
File "/home/cb27g11/Development/MainIridis_script.py", line 21, in the_main_event
run_madgraph()
File "/home/cb27g11/Development/MainIridis_script.py", line 38, in run_madgraph
subprocess.check_call(['python', './madgraph/bin/mg5_aMC mg_run_iridis.txt'])
File "/home/cb27g11/.conda/envs/MGenv/lib/python3.9/subprocess.py", line 373, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['python', './madgraph/bin/mg5_aMC mg_run_iridis.txt']' returned non-zero exit status 2.

需要明确的是,/home/cb27g11/Development肯定存在:

(MGenv) [cb27g11@green0155 Development]$ pwd
/home/cb27g11/Development

与涉及的各种文件一样:

(MGenv) [cb27g11@green0155 Development]$ ls
AD_noCharge_021020.tar.gz  NoChar_Allh_1.0_0.9  mg_run_basic.txt
MainIridis_script.py       NoChar_Allh_1_0.9    mg_run_iridis.txt
NoCh_h1Only.tar.gz         iridis_script.pbs    py.py
NoCh_h2Only.tar.gz         madgraph

包括mg5_aMC:

(MGenv) [cb27g11@green0155 Development]$ cd madgraph/
(MGenv) [cb27g11@green0155 madgraph]$ cd bin
(MGenv) [cb27g11@green0155 bin]$ ls
mg5  mg5_aMC  py.py

我不知道该怎么做,最终无论我怎么做,我仍然需要在另一个脚本中调用这个脚本文件(这次是bash(,以便将其提交到集群。

我不确定是否有可能完全避免使用python脚本并切换到bash,我怀疑至少其中一些需要保留在python中,但我是一名物理学家,而不是程序员,所以我很容易出错!以下是完整的脚本:

from __future__ import absolute_import, division, print_function
import numpy as np
import subprocess
import fileinput

tan_beta = np.arange(1, 21.2, 0.2) #Array of tan(beta) values
sin_bma = np.arange(0.9, 1.001, 0.001)#Array of sin(beta-alpha) values
hcm =  1.500000e+05 # Mass of charged Higgses in GeV
textfilepath = 'mg_run_iridis.txt' #path to txt file madgraph will use
Process = 'NoChar_Allh'

def the_main_event():
with open('mg_run_basic.txt','r') as old_card:
text =old_card.read() #stores string of old_card ready for editing
for i in range(0, len(tan_beta)):
tb = tan_beta[i] # loops over tan(beta) values
for j in range(0, len(sin_bma)):
sbma = sin_bma[j] # loops over sin(beta-alpha) values
make_input(tb, sbma, hcm, text)
print('about to run madgraph ' + str(tb) + ' ' + str(sbma))
run_madgraph()

def make_input(Tb, Sbma, Hcm, Text):
# inputs are the value of tan_beta, the value of sin(beta-alpha) values, the desired mass for the charged higgses and a string of text

with open(textfilepath, 'w') as new_card:
#simulation card, the .txt file that gets fed to madgraph
sim_card = Text.replace('TBV', str(Tb))
sim_card = sim_card.replace('SBMAV', str(Sbma))
sim_card = sim_card.replace('HCMV', str(Hcm))
sim_card = sim_card.replace('NAME', str(Process)+'_' + str(Tb) +'_' + str(Sbma))
new_card.write(sim_card) # saves new txt file for madgraph


def run_madgraph():
# starts madgraph and tells it to run mg5_aMC using mg_run_iridis.txt as a file for it.
print('starting....')
subprocess.check_call(['python', './madgraph/bin/mg5_aMC mg_run_iridis.txt'])


the_main_event()

从错误来看,它似乎将madgraph可执行文件(我不确定这是否是正确的术语,我指的是启动propgram的文件(视为一个模块?

任何帮助都将不胜感激!

尝试:

subprocess.run('python ./madgraph/bin/mg5_aMC mg_run_iridis.txt', shell=True)

请注意run部分。有关详细信息,请参阅文档。目前,您正在尝试调用subprocess模块本身,而不是该模块中的特定函数。

最新更新