使用 Anaconda 的 Spyder 时缩进有问题吗

Is there something issue in indentation, while using Anaconda's Spyder

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

此代码片段在 Anaconda 的 Spyder 中 运行 时弹出缩进错误。这是以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Panch\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\__init__.py", line 18, in <module>
    from .firefox.webdriver import WebDriver as Firefox  # noqa
  File "C:\Users\Panch\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 29, in <module>
    from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
  File "C:\Users\Panch\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 27, in <module>
    from .remote_connection import RemoteConnection
  File "C:\Users\Panch\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 34, in <module>
    from .errorhandler import ErrorCode
  File "C:\Users\Panch\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242
    raise exception_class(message, screen, stacktrace)
                                                     ^

IndentationError: unindent 不匹配任何外部缩进级别

如果我遗漏了什么,请告诉我。

这个错误信息...

IndentationError: unindent does not match any outer indentation level

...表示代码行中存在缩进问题。

显然,我在您发布的代码行中没有发现任何缩进问题。但是,有可能 Space 字符与您的 Tab.

混在一起

解决方案

根据您使用的 IDE 有不同的解决方案如下:

  • 尝试搜索并用几个空格替换所有选项卡,如 Spaces are the preferred method for indentation
  • 中你可以setunset 选中取消选中以使用制表符进行缩进:

    View --> Indentation --> Convert Indentation to Tabs
    
  • 要解决 tabs/spaces 问题,您可以使用 tabnanny,如下所示:

    python -m tabnanny your_python_file.py
    
  • 在 Python 编辑器中,select 所有 code/text 并执行以下操作:

    Go to Format -> Untabify Region
    
  • 如果您使用 ,请按 Esc 并执行以下操作以自动缩进所有内容并清除所有空格。

  • Atom,转到:

    Packages > Whitespace > Convert Spaces to Tabs
    

这是一种奇怪的行为,但可能文件 selenium\webdriver\remote\errorhandler.py 不知何故被破坏了。

您可以在具有"convert tabs to spaces"功能的文本编辑器中打开此文件,使用此功能并将其保存回同一文件。