两个Python脚本,相同的命令,一个有效,一个不起作用

时间:2019-05-16 15:15:23

标签: python subprocess

我正在编写一些半量子化学软件的脚本,但是遇到了令我感到困惑的问题。这两个脚本具有相同的形式,唯一的区别是命令。一种脚本有效,另一种有错误。

这两个python脚本具有相同的形式,并执行bash脚本来设置执行python2脚本的路径。一个脚本起作用,第二个脚本产生错误。当我在bash窗口中执行这些脚本时,它们都起作用。

有效的脚本是:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
calc_pdbqt

Created on Mon May 13 09:50:54 2019

copyrignt (c) 2019 Stephen P. Molnar, Ph.D.  All rights reserved
"""

import subprocess
with open('ligand') as infile:
    ligand = infile.read().strip().split()
for nvar in ligand:
    command = ["./pythonsh", "./prepare_ligand4.py",
        "-l", nvar + ".mol2",
        "-o", nvar + ".pdbqt" ]
    proc = subprocess.Popen(command, stdout=subprocess.PIPE)
    out, err = proc.communicate()
    print(out)
    print(err)
    if proc.returncode:
        print ('Subprocess FAILED:', proc.command)

生成错误的脚本是:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    """
    calc_pdf4

    Created on Mon May 13 09:50:54 2019

    copyrignt (c) 2019 Stephen P. Molnar, Ph.D.  All rights reserved
    """

    import subprocess
    with open('ligand') as infile:
        ligand = infile.read().strip().split()
    for nvar in ligand:
        command = ["./pythonsh", "./prepare_pdf4.py",
            "-l", nvar + ".pdbqt",
            "-ro", nvar + ".dpf" ]
        proc = subprocess.Popen(command, stdout=subprocess.PIPE)
        out, err = proc.communicate()
        print(out)
        print(err)
        if proc.returncode:
            print ('Subprocess FAILED:', proc.command)

错误是:

    runfile('/home/comp/Apps/Models/1-NerveAgents/Ligands/calc_pdf.py', wdir='/home/comp/Apps/Models/1-NerveAgents/Ligands')
    b''
    None
    /sdc1/Apps/MGLTools2-1.1/bin/python: can't open file './prepare_pdf4.py': [Errno 2] No such file or directory
    Traceback (most recent call last):

      File "<ipython-input-1-078e132fa938>", line 1, in <module>
    runfile('/home/comp/Apps/Models/1-NerveAgents/Ligands/calc_pdf.py', wdir='/home/comp/Apps/Models/1-NerveAgents/Ligands')

      File "/home/comp/Apps/miniconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 824, in runfile
        execfile(filename, namespace)

      File "/home/comp/Apps/miniconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile
        exec(compile(f.read(), filename, 'exec'), namespace)

      File "/home/comp/Apps/Models/1-NerveAgents/Ligands/calc_pdf.py", line 23, in <module>
        print ('Subprocess FAILED:', proc.command)

    AttributeError: 'Popen' object has no attribute 'command'

Pythonsh是一个bash脚本,是MGL_Tools(http://mgltools.scripps.edu/)的一部分,它是我试图在我的两个脚本中实现的prepare_ligand4.py和prepare_dpf4.py的来源。 MGL_Tools是AutoDock的GUI,而我已经使用了至少25年。 (MGL_Tools和AutoDock都是开源的,已经被广泛使用。)

pythonsh的功能是建立一个使用python2的环境并设置许多路径。如果我在bash shell中调用pythonsh,我将得到:

comp@AbNormal:/sdc1/Apps/Models/1-NerveAgents/Scripts$ ./pythonsh
setting PYTHONHOME environment
Python 2.5.6 (r256:88840, Nov  6 2012, 15:29:26)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

问题可能不是prepare_dpf4.py的调用,因为:

comp@AbNormal:/sdc1/Apps/Models/1-NerveAgents/Ligands$ ./pythonsh ./prepare_dpf4.py
prepare_dpf4.py: ligand and receptor filenames
                    must be specified.
Usage: prepare_dpf4.py -l pdbqt_file -r pdbqt_file
    -l ligand_filename
    -r receptor_filename

Optional parameters:
    [-o output dpf_filename]
    [-i template dpf_filename]
    [-x flexres_filename]
    [-p parameter_name=new_value]
    [-k list of parameters to write]
    [-e write epdb dpf ]
    [-v] verbose output
    [-L] use local search parameters
    [-S] use simulated annealing search parameters
    [-s] seed population using ligand's present conformation

Prepare a docking parameter file (DPF) for AutoDock4.

   The DPF will by default be <ligand>_<receptor>.dpf. This
may be overridden using the -o flag.

然后:

/sdc1/Apps/Models/1-NerveAgents/Ligands$ ./pythonsh ./prepare_dpf4.py -l VM.pdbqt -ro apo-1acl.pdbqt

which results in:

/sdc1/Apps/Models/1-NerveAgents/Ligands$ l *.dpf
-rw-r--r-- 1 comp comp 2892 May 16 08:20 VM_o.dpf

现在,这里唯一的问题是文件名应该是'VM.dpf'而不是'VM_o.dpf'。但是,我稍后会担心。顺便说一句,文件内容是正确的。

生成错误的脚本是执行prepare_dpr4.py的脚本。

0 个答案:

没有答案