Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python缩进错误以匹配外部缩进级别_Python - Fatal编程技术网

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

Python缩进错误以匹配外部缩进级别,python,Python,我有以下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 ou

我有以下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()
执行时我得到了错误

缩进错误:未缩进与任何外部缩进级别不匹配


我已经匹配了外部缩进级别,但是为什么我仍然会出现这个错误。

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

在Python中,代码的缩进与许多其他语言不同。在您的例子中,您没有缩进类声明的内容:

导入子流程,流程\实用程序

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()

我已经编辑了这个问题。我尝试了同样的方法,但仍然出现了这个错误
def run_test
下面的行仍然离左边太远请确保您没有混合制表符和空格。这对我很有效。这是你的完整代码吗?我也有一个主类。编辑代码然后你应该检查你是否意外地混合了制表符和空格。不,我没有,我检查了。