简体   繁体   English

缩进错误:意外缩进python

[英]Indentationerror: unexpected indent python

I looked up the reasons for indent error, ie four spaces instead of tab and tried deleting all the spaces and replacing them myself with 4 spaces to no avail.我查找了缩进错误的原因,即四个空格而不是制表符,并尝试删除所有空格并用 4 个空格替换它们,但无济于事。

The error comes on line 2. With the arrow under the p of print.错误出现在第 2 行。打印 p 下的箭头。

 filepath = "F:\\marketing II\\programming\\python\\projects\\testpic.jpg" print ("Uploading " + filepath) insta = InstagramSession() if insta.login(user, pass): media_id = insta.upload_photo("/tmp/small.jpg") print media_id if media_id is not None: insta.configure_photo(media_id, "")

you do not need to indent in that line.您不需要在该行中缩进。 your code should look like this:您的代码应如下所示:

filepath = "F:\marketing II\programming\python\projects\testpic.jpg"
print ("Uploading " + filepath)
insta = InstagramSession()
if insta.login(user, pass):
    media_id = insta.upload_photo("/tmp/small.jpg")
    print media_id
    if media_id is not None:
        insta.configure_photo(media_id, "")

Remove the indent, it should look like this:删除缩进,它应该是这样的:

filepath = "F:\marketing II\programming\python\projects\testpic.jpg"
print ("Uploading " + filepath)
insta = new InstagramSession()
if insta.login(user, pass):
    media_id = insta.upload_photo("/tmp/small.jpg")
    print media_id
    if media_id is not None:
        insta.configure_photo(media_id, "")

Yse indents only after instructions like loops, if statements, while expression. Yse 仅在循环、if 语句、while 表达式等指令之后缩进。 This is because in python you cond use curly bracets, but indents.这是因为在 python 中你会使用花括号,但缩进。 You can find more info about indentions here .您可以在此处找到有关缩进的更多信息。

Update :更新

Also you are using a class InstagramSession, so you need to place a keyword new before it.此外,您正在使用 InstagramSession 类,因此您需要在它之前放置一个关键字 new。

In Python, indentation marks code blocks.在 Python 中,缩进标记代码块。 They start under a line that ends with a colon : , and they end when the indentation ends.它们从以冒号:结尾的行开始,并在缩进结束时结束。 Since your first line which assigns the variable filepath does not start a block, you must not indent further below it.由于您分配变量文件filepath第一行没有开始一个块,您不能在它下面进一步缩进。 Either adjust your indentation or insert a line that does open a block, such as if True: .调整缩进或插入一行打开块的行,例如if True:

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

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