简体   繁体   English

使用python subprocess.call运行命令失败,但在键入cmd.exe时可以正常运行

[英]Running command with python subprocess.call fails but works correctly when typed into cmd.exe

So the background on this is that I'm trying to setup a Cocos-2dx v3.0 project. 因此,背景是我正在尝试设置一个Cocos-2dx v3.0项目。 I've gotten the python script that creates the project to run and now I'm trying to build and run the project so I can start development on a game. 我已经获得了创建要运行的项目的python脚本,现在我正在尝试构建和运行该项目,以便可以开始在游戏上进行开发。 The issue I'm running into, as per the question, is that one of the commands issued by subprocess.call fails every time the build and run python script is run. 按照这个问题,我遇到的问题是每次运行build and run python脚本时,subprocess.call发出的命令之一都会失败。 When I run the exact command being passed to it in the shell myself however, it runs perfectly. 但是,当我自己在shell中运行传递给它的确切命令时,它运行完美。

The method in python calling subprocess.call: python中调用subprocess.call的方法:

@staticmethod
def run_cmd(command, verbose):
    if verbose:
        Logging.debug("running: '%s'\n" % ''.join(command))
    else:
        log_path = CCPlugin._log_path()
        command += ' >"%s" 2>&1' % log_path    
    print "CALLING run_cmd"
    ret = subprocess.call(command, shell=True)
    if ret != 0:
        message = "Error running command, return code: %s" % str(ret)
        if not verbose:
            message += ". Check the log file at %s" % log_path
        raise CCPluginError(message)

This is used multiple times in the script and succeeds every other time. 该脚本在脚本中多次使用,并且每隔一次成功。

The command that is failing when called from python but which runs fine when directly input to cmd: 从python调用时失败的命令,但直接输入到cmd时运行良好:

"C:\Users\Patrick\Downloads\apache-ant-1.9.4-bin\apache-ant-1.9.4\bin\ant" clean debug -f C:\HyperFusion\RuneWars\RuneWars\proj.android\build.xml -Dsdk.dir="C:\Users\Patrick\COPAP\Software\adt-bundle-windows-x86-20130522\sdk"

Any and all help would be appreciated. 任何和所有帮助将不胜感激。 I've done a bunch of googling but haven't come across anything that would seem to help. 我做了很多谷歌搜索,但是没有发现任何可能会有所帮助的东西。 I'm new to python (and really didn't expect to have to debug it since these are only setup scripts provided by Cocos) so my apologies if I'm missing something simple here. 我是python的新手(由于这些只是Cocos提供的安装脚本,因此我真的没想到不必调试它),如果我在这里缺少一些简单的东西,我深表歉意。

OS is Windows 7 if that has any bearing on the issue. 如果与该问题有关,则操作系统为Windows 7。

Thanks! 谢谢!

There was a reason why I've asked about 'verbose' flag. 我被问及“冗长”标志的原因是有原因的。 If it's set to 'False', try to comment out one line as shown below: 如果将其设置为“ False”,请尝试注释掉一行,如下所示:

 else:
    log_path = CCPlugin._log_path()
    # command += ' >"%s" 2>&1' % log_path    

What could happen there was that the next command won't be able to open the log file for writing because it has been already opened by the previous command.It could be a good explanation why every other command succeeds as you wrote. 可能发生的情况是下一个命令将无法打开日志文件进行写入,因为上一个命令已经打开了该文件,这可能很好地解释了为什么其他每个命令在您编写时都会成功。

Just a hypothesis that should be easy to check. 只是一个应该易于检查的假设。 Good luck! 祝好运!

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

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