IndentationError:意外取消缩进
Posted
技术标签:
【中文标题】IndentationError:意外取消缩进【英文标题】:IndentationError: unexpected unindent 【发布时间】:2018-02-04 00:22:55 【问题描述】:不久前开始编码,大约一个月左右。我目前正在为 Discord 编写一个机器人,一切正常,直到我在添加新命令后尝试运行该机器人时收到此错误消息:
Traceback (most recent call last):
File "C:\Users\Jeriel\Desktop\JerryBot\run.py", line 162, in main
from musicbot import MusicBot
File "C:\Users\Jeriel\Desktop\JerryBot\musicbot\__init__.py", line 1, in <modu
le>
from .bot import MusicBot
File "C:\Users\Jeriel\Desktop\JerryBot\music\bot.py", line 2094
if __name__ == "__main__":
^
IndentationError: unexpected unindent
它是在我添加这个之后开始的。我检查了这些之前的每一行,我在任何地方都找不到一个取消缩进:
async def kick(message,*args):
"""Kicks the specified user from the server"""
if len(message.mentions) < 1:
return False
if message.channel.is_private:
msg = await client.send_message(message.channel,'Users cannot be kicked/banned from private channels.')
asyncio.ensure_future(message_timeout(msg, 40))
return
if not message.channel.permissions_for(message.server.get_member(client.user.id)).kick_members:
msg = await client.send_message(message.channel, message.author.mention + ', I do not have permission to kick users.')
asyncio.ensure_future(message_timeout(msg, 40))
return
members = []
if not message.channel.is_private and message.channel.permissions_for(message.author).kick_members:
for member in message.mentions:
if member != message.author:
try:
await client.kick(member)
members.append(member.name)
except:
pass
else:
msg = await client.send_message(message.channel, message.author.mention + ', You should not kick yourself from a channel, use the leave button instead.')
asyncio.ensure_future(message_timeout(msg, 40))
else:
msg = await client.send_message(message.channel, message.author.mention + ', I do not have permission to kick users, or this is a private message channel.')
asyncio.ensure_future(message_timeout(msg, 40))
msg = await client.send_message(message.channel,'Successfully kicked user(s): ``'.format('`, `'.join(members)))
asyncio.ensure_future(message_timeout(msg, 60))
@register('ban','@<mention users>',owner=True)
async def ban(message,*args):
"""Bans the specified user from the server"""
if len(message.mentions) < 1:
return False
if message.channel.is_private:
msg = await client.send_message(message.channel,'Users cannot be kicked/banned from private channels.')
asyncio.ensure_future(message_timeout(msg, 40))
return
if not message.channel.permissions_for(message.server.get_member(client.user.id)).ban_members:
msg = await client.send_message(message.channel, message.author.mention + ', I do not have permission to ban users.')
asyncio.ensure_future(message_timeout(msg, 40))
return
members = []
if message.channel.permissions_for(message.author).ban_members:
for member in message.mentions:
if member != message.author:
try:
await client.ban(member)
members.append(member.name)
except:
pass
else:
msg = await client.send_message(message.channel, message.author.mention + ', You should not ban yourself from a channel, use the leave button instead.')
asyncio.ensure_future(message_timeout(msg, 40))
else:
msg = await client.send_message(message.channel, message.author.mention + ', I do not have permission to ban users, or this is a private message channel.')
asyncio.ensure_future(message_timeout(msg, 40))
msg = await client.send_message(message.channel,'Successfully banned user(s): ``'.format('`, `'.join(members)))
asyncio.ensure_future(message_timeout(msg, 30))
@register('bans',alias='bannedusers')
@register('bannedusers')
if __name__ == "__main__":
bot = JerryBot()
bot.run("---")
【问题讨论】:
错误信息指向if __name__ == "__main__":
。为什么该行相对于其余代码没有缩进?
您是复制并粘贴代码,还是自己输入?如果您复制并粘贴它,可能是由于您使用制表符在某些地方缩进,而在其他地方使用空格来缩进的问题。这样做总是很有趣……
@Blorgbeard 我认为 OP 想使用 if __name__ == "__main__":
块作为主要功能,如果是这样,那么它必须不缩进
【参考方案1】:
你的问题在这里:
@register('bans',alias='bannedusers')
@register('bannedusers')
if __name__ == "__main__":
bot = JerryBot()
bot.run("---")
装饰器语法需要在@
行下定义函数,该函数必须与@
处于相同的缩进级别。换句话说,你不能在那里有你的if
声明。您需要一个函数定义。缩进只是它发现的第一个问题。如果你缩进你的if
语句,你会得到一个不同的错误。
我不知道您是否在此处错误地包含了 @
行,或者您是否省略了您打算放置在那里的函数定义。根据您的目的,编写函数或删除 @
行。
【讨论】:
谢谢!我就是这样做的,它似乎解决了缩进问题,但是一旦我这样做,就会出现:File "C:\Users\Jeriel\Desktop\JerryBot\musicbot\bot.py", line 2018 async def kick(message,*args): ^ TabError: inconsistent use of tabs and spaces in indentation C:\Users\Jeriel\Desktop\JerryBot>
关于如何解决这个问题的任何想法?【参考方案2】:
您是否可以混合使用制表符和空格?这是python初学者的常见错误。从技术上讲,您可以使用其中任何一种,但不能混合使用。我对此的解决方案(我使用 vim 作为 IDE)是在我的 .vimrc 中将 tab 键设置为实际上是 4 个空格。
您可以尝试在 vim 中通过输入 gg=G
并在命令模式下按 Enter 键重新插入。
【讨论】:
以上是关于IndentationError:意外取消缩进的主要内容,如果未能解决你的问题,请参考以下文章
IndentationError: unindent 不匹配任何外部缩进级别
IndentationError:unindent 不匹配任何外部缩进级别我啥都不懂[关闭]
IndentationError:期望在 requests.adapter 中有一个缩进块