【发布时间】:2014-10-08 19:46:31
【问题描述】:
我有一个需要用 Python 运行的 Perl 脚本,我一直在尝试使用 subprocess 来完成它,但没有成功。我可以让命令在命令行上正常运行,但子进程无法让它工作。
如果我有这样的 Perl 脚本:
#!/usr/bin/perl
use strict;
use warnings;
my $name = shift;
print "Hello $name!\n";
我能够像这样在命令行上成功运行命令
C:\current\path> perl test.pl world
>>>Hello world!
但是当我尝试使用子进程调用相同的命令时,我得到了这个错误
cmd = 'perl test.pl world'
pipe = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
>>>"'perl' is not recognized as an internal or external command, operable program or batch file."
我尝试了将 cmd 创建为列表的不同组合
cmd = ['perl','test.pl','world']
以及给子进程 Perl 的绝对路径
cmd = ['C:\path\to\perl.exe','test.pl','world']
但没有任何东西可以让它工作。没有 Perl,我可以让子进程正常运行
pipe = subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE)
pipe.stdout.read()
>>>Volume in drive C is Windows7_OS....
我绝对确定 Perl 在我的 PATH 中,因为就像我说的那样,我可以在命令行中很好地调用该命令。我查看了建议检查os.environ["COMSPEC"] 的不同帖子,将Perl 添加到子进程环境中,但没有任何效果。
编辑:我也无法让该命令与任何其他子进程方法一起使用:check_output() 返回一个空字节字符串,call() 返回 1。
我们将不胜感激任何形式的修复或替代解决方案。我在 Windows 7 64 位上运行 Python 3.4。我也尝试过 32 位和 64 位 Python,但无济于事。
更新:
我已经能够让它在 Mac 上运行,但有一个区别:我不能拥有 shell=True。除此之外,我想要的任何subprocess 函数都可以使用,包括Popen、check_output 和call。
所以我想这是一个 Windows 问题。我试过不在我的 Windows 机器上设置shell=True,但我总是收到这个错误:
WindowsError: [Error 193] %1 is not a valid Win32 application
更新 2: 我也尝试创建一个运行该命令的 .bat 文件,但我也遇到了同样的错误,我尝试只调用 Perl。
pipe = subprocess.Popen('test.bat',shell=True,stdout=subprocess.PIPE)
>>>"'test.bat' is not recognized as an internal or external command, operable program or batch file."
test.bat 只有一行:
echo Hello, World
【问题讨论】:
标签: python perl subprocess windows-7-x64