Unexpected Indent Error in Visual Studio Python Interactive Window

SunMesa 1 Reputation point
2021-09-07T18:09:32.477+00:00

I'm trying to execute a short snippet of python code in the VS 2017 Python Interactive Window. First I'll show the code that works as expected:

for j in range(0,3):
print('j = ',j)
mylist = [ 11*j+i for i in range (0,11) ]
print(mylist)

This produces the following output:

j = 0
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
j = 1
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
j = 2
[22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]

just as it should. Then I decided I wanted the output of mylist to be just a list of space-separated integers (no commas or brackets), by using the unpack operator (*). The code differs from the above only in the insertion of an asterisk before mylist in the print statement:

for j in range(0,3):
print('j = ',j)
mylist = [ 11*j+i for i in range (0,11) ]
print(*mylist)

This results in the following output:

j =  0
j =  1
j =  2
>>>     print(*mylist)

  File "<stdin>", line 1
    print(*mylist)
    ^
IndentationError: unexpected indent

All the indents are consistent, either 4 blank spaces or a tab of length 4. I've tried None, Block, and Smart for indenting options, and both Insert Spaces and Keep Tabs for tab options, nothing fixes the error.

To top it off, when the code with the unpack operator is executed in a windows command prompt, it works perfectly! I.e., save the python snippet as TestUnpack.py, then type "py -m TestUnpack" in a windows command prompt... here's the output:

j =  0
0 1 2 3 4 5 6 7 8 9 10
j =  1
11 12 13 14 15 16 17 18 19 20 21
j =  2
22 23 24 25 26 27 28 29 30 31 32

just as expected. Does anyone know what's causing the unexpected indent error in the VS Python interactive window? Thanks for any info!

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,594 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. PengGe-MSFT 3,331 Reputation points
    2021-09-08T05:34:31.043+00:00

    Hi @SunMesa

    Welcome to Microsoft Q&A!

    I successfully ran your code in Visual Studio 2017, whether it is four spaces or a Tab, it runs successfully.

    for j in range(0,3):  
        print('j = ',j)  
        mylist = [ 11*j+i for i in range (0,11) ]  
        print(*mylist)  
    

    130045-image.png

    You can try to create a new project to run this code.
    *
    Update:
    According to your statement, I read this document and tested it, and I found this result:

    When you manually enter the code, it can run correctly regardless of whether the code contains an unpack operator, but when the code contains an unpack operator, copy and paste the code and then run it will cause an error. Obviously, the Python Interactive window treats the line where the unpack operator is located as the beginning of new code.

    I think this is a bug. I suggest you can start a new thread and report this issue to VS Product Team in our Developer Community with some reproduced or detailed information. if you do report this issue, please share the link here, other forum members who are interested or meet the same or similar issue can go to vote for this thread.

    Sincerely,
    Peng
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread

    0 comments No comments

  2. SunMesa 1 Reputation point
    2021-09-08T17:05:07.797+00:00

    Hi @PengGe-MSFT ,

    Thanks for your feedback. We are clearly launching the code from different windows. In your VS project, go to View/Other Windows/Python Interactive Window. Then cut and paste the above code (with the unpack operator) into the interactive window and see what happens... this is where I get the 'unexpected indent' error message. Then, in that same interactive window, cut and paste in the same code but without the unpack operator (i.e., just 'mylist' rather than '*mylist'). This will execute just as expected (or at least it does for me), indicating that the VS interactive window is correctly interpreting indents and the structure of the 'for j' loop. Evidently something about the unpack operator is causing a problem in this environment (but not in, say, a Windows command prompt).


  3. SunMesa 1 Reputation point
    2021-09-11T23:53:08.013+00:00

    Hi @PengGe-MSFT ,

    My apologies for the delayed reply. Most other forums give notification by email when a post has been replied to, but this one apparently does not.

    I see also that the code snippets in my original post got inserted as text rather than code and lost the indentation, which must have been confusing.

    In any case, thanks for your update and the link to the VS Interactive Window documentation. After reviewing it, I must agree that this seems like a bug. Interestingly, when I open the python code snippet with the unpack operator as a Visual Studio project, then demarcate it as a code cell (using a preceding comment line starting with "#%%"), and then send it to the python interactive window using Ctrl+Enter, I do not get the "Unexpected Indent" error, but it nevertheless does not execute correctly. The output is:

    j =  0  
    j =  1  
    j =  2  
    >>> print(*mylist)  
    22 23 24 25 26 27 28 29 30 31 32  
    

    which indicates that the print(*mylist) command is, as you say, being executed as a new line of code, rather than being incorporated into the "for j" loop.

    I'll try to properly compose this into a question/issue for the VS Product team as you suggest, and will post a link to it here if I receive any substantive replies.