admin管理员组

文章数量:1122846

I have a very weird issue affecting my code. I'm getting set up on a new machine, and in VS Code the Python terminal is passing different code than what I wrote in the editor. I'm not sure if there is a setting that I need to change, but I never had this issue on any of the other machines that I've worked on.

The terminal seems to be passing indentions that do not exist in the written code if I include the code in a function. All of this code is highlighted in the code editor and run by hitting Shift+Enter. When I run the function, it gives an Unexpected Indent error.

If I have the code:

var1 = 1
var2 = 2
print(var1+var2)

it works.

If I wrap it in a function:

def test_function():
    var1 = 1
    var2 = 2
    print(var1+var2)

it does not.

When I look in the terminal, the code that is passed looks like this:

This is not the way the code is written:

I have ensured that the indention setting is set to four. I've tried this with both tabs and spaces.

The only thing that seems to work is if I remove all the white space, and now it works when I highlight it.

I have tried searching for settings to change, but nothing has worked.

Python 3.13.0 VS code 1.96.2

I have a very weird issue affecting my code. I'm getting set up on a new machine, and in VS Code the Python terminal is passing different code than what I wrote in the editor. I'm not sure if there is a setting that I need to change, but I never had this issue on any of the other machines that I've worked on.

The terminal seems to be passing indentions that do not exist in the written code if I include the code in a function. All of this code is highlighted in the code editor and run by hitting Shift+Enter. When I run the function, it gives an Unexpected Indent error.

If I have the code:

var1 = 1
var2 = 2
print(var1+var2)

it works.

If I wrap it in a function:

def test_function():
    var1 = 1
    var2 = 2
    print(var1+var2)

it does not.

When I look in the terminal, the code that is passed looks like this:

This is not the way the code is written:

I have ensured that the indention setting is set to four. I've tried this with both tabs and spaces.

The only thing that seems to work is if I remove all the white space, and now it works when I highlight it.

I have tried searching for settings to change, but nothing has worked.

Python 3.13.0 VS code 1.96.2

Share Improve this question edited Jan 3 at 18:54 PM 77-1 13.3k21 gold badges70 silver badges115 bronze badges asked Jan 3 at 18:42 Stephen JuzaStephen Juza 2931 gold badge3 silver badges12 bronze badges 2
  • Which operating system? Is this a saved file or are you copy/pasting into a python shell? In VSCode --> Settings, what is the eol configuration? – tdelaney Commented Jan 3 at 19:10
  • 1 I saw some similar posts with Python 3.13. Try again with 3.12. – vimchun Commented Jan 3 at 22:21
Add a comment  | 

1 Answer 1

Reset to default 1

I am able to reproduce the problem on command prompt, Python version 3.13.1.

As vimchun said, switching to version 3.12.X will resolve the indentation issue.

本文标签: