繁体   English   中英

Python缩进错误以匹配外部缩进级别

[英]Python Indentation error to match the outer indentation level

我有以下python代码。

import subprocess, process_utility

class PhantomasProcessor:

    def run_test(self, url):
    """
    Method to run a test through Phantomas.

    Args:
        url for which you want to run the test.

    Returns:
         The json output for 
     """

    command = "phantomas " + url + "--har=test.har"
    result = ProcessUtility.execute_command(command)
    return result

def main():
phantomas_processor = PhantomasProcessor()
print phantomas_processor.run_test("www.amazon.com")

if __name__ == "__main__":
    main()

执行后,我得到了错误。

IndentationError: unindent does not match any outer indentation level

我已经匹配了外部缩进级别,但是为什么我仍然不断收到此错误。

您的def和下面的所有行都应向右移动一个缩进(4个空格/ 1个制表符/所用的内容)

在Python中,与许多其他语言不同,代码的缩进很重要。 在您的情况下,您想缩进类声明的内容:

导入子流程process_utility

class PhantomasProcessor:

    def run_test(self, url):
        """
        Method to run a test through Phantomas.

        Args:
             url for which you want to run the test.

        Returns:

             The json output for 
        """

        command = "phantomas " + url + "--har=test.har"
        result = execute_command(command)
        return result

干得好。

import subprocess, process_utility

class PhantomasProcessor:

    def run_test(self, url):
    """
    Method to run a test through Phantomas.

    Args:
        url for which you want to run the test.

    Returns:
         The json output for 
     """

    command = "phantomas " + url + "--har=test.har"
    result = ProcessUtility.execute_command(command)
    return result

def main():
    phantomas_processor = PhantomasProcessor()
    print phantomas_processor.run_test("www.amazon.com")

if __name__ == "__main__":
    main()

您需要缩进如下:

import subprocess, process_utility

class PhantomasProcessor:

    def run_test(self, url):
        """
        Method to run a test through Phantomas.

        Args:
            url for which you want to run the test.

        Returns:
            The json output for 
        """

        command = "phantomas " + url + "--har=test.har"
        result = ProcessUtility.execute_command(command)
        return result

def main():
    phantomas_processor = PhantomasProcessor()
    print phantomas_processor.run_test("www.amazon.com")

if __name__ == "__main__":
    main()

暂无
暂无

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

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