简体   繁体   English

subprocess.call在Windows上使用cygwin而不是cmd

[英]subprocess.call using cygwin instead of cmd on Windows

I'm programming on Windows 7 and in one of my Python projects I need to call bedtools , which only works with Cygwin on Windows. 我在Windows 7上进行编程,在我的一个Python项目中,我需要调用bedtools ,它只适用于Windows上的Cygwin。 I'm new to Cygwin, installed the default version + everything needed for bedtools and then used Cygwin to install bedtools by using make as described in the installation instructions . 我是Cygwin的新手,安装了默认版本+ bedtools所需的一切,然后使用Cygwin按照安装说明中的说明使用make安装bedtools。

$ tar -zxvf BEDTools.tar.gz
$ cd BEDTools-<version>
$ make

When I use the Cygwin terminal to call it manually like below, it works without problem and the output file contains the correct result. 当我使用Cygwin终端手动调用它时,如下所示,它没有问题,输出文件包含正确的结果。

bedtools_exe_path intersect -a gene_bed_file -b snp_bed_file -wa -wb > output_file

But when I use subprocess.call in my program it seems to use Windows cmd instead of Cygwin, which doesn't work. 但是当我在我的程序中使用subprocess.call ,它似乎使用Windows cmd而不是Cygwin,这不起作用。

arguments = [bedtools_exe_path, 'intersect', '-a', gene_bed_file, '-b',
             snp_bed_file, '-wa', '-wb', '>', output_file]
return_code = suprocess.call(arguments)

Results in no output file and a return code of 3221225781 . 结果没有输出文件,返回码为3221225781


arguments = [bedtools_exe_path, 'intersect', '-a', gene_bed_file, '-b',
             snp_bed_file, '-wa', '-wb', '>', output_file]
return_code = suprocess.call(arguments, shell=True)

Results in an empty output file and a return code of 3221225781 . 结果为空输出文件,返回码为3221225781


cygwin_bash_path = 'D:/Cygwin/bin/bash.exe'
arguments = [cygwin_bash_path, bedtools_exe_path, 'intersect', '-a', gene_bed_file, '-b',
             snp_bed_file, '-wa', '-wb', '>', output_file]
return_code = suprocess.call(arguments)

Results in no output file, a return code of 126 and 结果没有输出文件,返回码为126

D:/BEDTools/bin/bedtools.exe: D:/BEDTools/bin/bedtools.exe: cannot execute binary file

arguments = [cygwin_bash_path, bedtools_exe_path, 'intersect', '-a', gene_bed_file, '-b',
             snp_bed_file, '-wa', '-wb', '>', output_file]
return_code = suprocess.call(arguments, shell=True)

Results in an empty output file, a return code of 126 and 结果为空输出文件,返回码为126

D:/BEDTools/bin/bedtools.exe: D:/BEDTools/bin/bedtools.exe: cannot execute binary file

Any ideas how I can get it to work? 任何想法我怎么能让它工作?

Imagine you want to run a Linux command from Windows. 想象一下,您想从Windows运行Linux命令。 You could install Linux into a VM and run commands via ssh (Putty/plink on Windows): 您可以将Linux安装到VM中并通过ssh(Windows上的Putty / plink)运行命令:

#!/usr/bin/env python
import subprocess

cmd = [r'C:\path\to\plink.exe', '-ssh', 'user@vm_host', '/path/to/bedtools']
with open('output', 'wb', 0) as file:
    subprocess.check_call(cmd, stdout=file)

Cygwin provides run command that allows to run commands directly: Cygwin的提供run命令,允许直接运行命令:

cmd = [r'C:\cygwin\path\to\run.exe', '-p', '/path/to/', 'bedtools', 
        '-wait', 'arg1', 'arg2']

Note: Python script is run from Windows in both cases. 注意:在这两种情况下都会从Windows运行Python脚本。 bedtools is Linux or Cygwin (non-Windows) command here and therefore you should provide POSIX paths. bedtools是Linux或Cygwin(非Windows)命令,因此您应该提供POSIX路径。

The following works without a problem. 以下工作没有问题。 The " does not need to be escaped. "不需要逃脱。

argument = 'sh -c \"' + bedtools_exe_path + ' intersect -a ' + gene_bed_file + 
           ' -b ' + snp_bed_file + ' -wa -wb\"'

with open(output_file, 'w') as file:
    subprocess.call(argument, stdout=file)

Using the following works as well: 使用以下工作:

argument = 'bash -c \"' + bedtools_exe_path + ' intersect -a ' + gene_bed_file + 
           ' -b ' + snp_bed_file + ' -wa -wb\"'

with open(output_file, 'w') as file:
    subprocess.call(argument, stdout=file)

With: 附:

bedtools_exe_path = 'D:/BEDTools/bin/bedtools.exe'
gene_bed_file = 'output/gene.csv'
snp_bed_file = 'output/snps.csv'
output_file = 'output/intersect_gene_snp.bed'

Using the path to the cygwin bash.exe ( D:/Cygwin/bin/bash.exe ) instead of bash or sh does not work. 使用cygwin bash.exe( D:/Cygwin/bin/bash.exe )的路径而不是bashsh不起作用。

Thank you, eryksun, Padraic Cunningham and JF Sebastian. 谢谢你,eryksun,Padraic Cunningham和JF Sebastian。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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