简体   繁体   中英

Indentation error when using subprocess in Python 2.7

I have tried using a function defined using subprocess but I get an indentation error depending on where I put it on my code. A minimal example of my code is:

import subprocess

def runsafe(job,args):
    jobs=[job];
    for arg in args:
        jobs.append(arg)
    proc=subprocess.Popen(jobs,stdout=subprocess.PIPE)
    return proc.stdout.readlines()

if __name__=="__main__":
        runsafe("mkdir","Try")
    A=0
    B=7

This works and gives me the correct input (this code just creates three new folders called T, r, y where it's executed) but the indentation seems weird to me. A and B are just two constants that I will use later in the code.

For me the most natural would be to write:

import subprocess

def runsafe(job,args):
    jobs=[job];
    for arg in args:
        jobs.append(arg)
    proc=subprocess.Popen(jobs,stdout=subprocess.PIPE)
    return proc.stdout.readlines()

if __name__=="__main__":
    runsafe("mkdir","Try")
    A=0
    B=7

If I do this I just get:

IndentationError: 'unexpected indent'

Why is it necessary to indent (again) the line where I call runsafe? Shouldn't it work if I use it like in the second example?

You are very likely mixing tabs and spaces in your indentation. With Python 2 this often leads to weird behavior because mixing tabs and spaces actually works. But it doesn't work in the way you would maybe expect: For Python 2, a tab is equivalent to 8 spaces . So for evaluating the indentation level, every tab is replaced by 8 spaces.

So when you have an indentation like this (where a . is a space, and ---→ is a tab):

def test():
........pass
---→pass

then it might look weird but it is valid since everything is indented using 8 spaces.

So you should make sure that you are using consistent indentation (it doesn't matter whether it's spaces or tabs—as long as it's consistent).

Btw.: In Python 3 this is no longer allowed and will throw a syntax error (actually a TabError ) with an actually helpful message: TabError: inconsistent use of tabs and spaces in indentation .

Your code mixes tabs and spaces for indentation. While this was possible and allowed in Python 2, Python 2 assumes tab size of 8 spaces . If you had an editor that has a setting to turn on visible whitespace, you'd see that one of the lines is indented with 8 spaces, and the other 2 with a single TAB :

if __name__=="__main__":
........runsafe("mkdir","Try")
--->A=0
--->B=7

That is, in your editor you had the tab display default to 4 spaces, while Python 2 expects 8 spaces to match a TAB .

As the tab size cannot be agreed on, yet in Python the indentation is significant, use of tabs for indentation is not considered best practice in either Python 2 or Python 3.

Tabs should be used solely to remain consistent with code that is already indented with tabs:

And mixing tabs and spaces in a way that makes indentation depend on the worth of a tab expressed in spaces is very wrong.


In Python 2 the tab size is 8, so indentation of                              TAB counts the same as 8 spaces or single TAB . Unfortunately there are many code editors that do not realize this, and instead use a different tab size for Python 2 code. In Python 3 OTOH, a TAB is only matched by another TAB . A good Python 3 editor would then mark inconsistent use of tabs in indent as syntax errors.

Thus the only consistent way of using tabs is using only tabs for indentation. In Python 2 there is a switch for that; you can start your Python interpreter with option -tt , or put it on the #! line after the python command, which will cause Python 2 to throw an error if it ever meets a line which makes the indent depend on tab size.

However, if you're writing new code, please be advised that the Python community almost uniformly follows the PEP 8 coding conventions: indentations are 4 spaces, and no tabs are used . In my experience, it is only the ever so odd legacy project that does not obey this guideline. Nearly all open-source projects these days that have coding conventions also require 4-space indentation, as well as other aspects of PEP 8 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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