pythonvisual-studio-codeindentation

Strange indentation errors when running Python code with Shift+Enter in VS Studio Code


I recently installed VS Code and Python on a new computer and am running into unusual behavior when trying to run a simple try/except block. Take the following test line of code:

try:
    1 / 0
except:
    print("Caught an error!")

If I highlight the code above and press Shift + Enter to execute, I will receive an indentation error as pictured:

I know this is not an indentation error and I can run this same code on one of my other computers and editors without issue. In fact, I can run similarly indented code without issues such as:

for i in range(3):
    print(i)

I can confirm that I am not mixing up tabs and spaces and can also confirm that both lines of code have the exact same indentation when run in VS Code.

I experienced a similar issue trying to use tkinter to create a grid using a for loop where, despite copying and pasting an example verbatim from W3 schools, I received the same sort of indentation error. What could be causing this?


Solution

  • This is a known issue with Python 3.13. Use python 3.12 will solve it.