question

SunMesa-7088 avatar image
0 Votes"
SunMesa-7088 asked PengGe-MSFT commented

Unexpected Indent Error in Visual Studio Python Interactive Window

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!

vs-general
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PengGe-MSFT avatar image
0 Votes"
PengGe-MSFT answered PengGe-MSFT edited

Hi @SunMesa-7088

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



image.png (18.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SunMesa-7088 avatar image
0 Votes"
SunMesa-7088 answered PengGe-MSFT commented

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).

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @SunMesa-7088
I updated my answer, please check it.

0 Votes 0 ·
SunMesa-7088 avatar image
0 Votes"
SunMesa-7088 answered PengGe-MSFT commented

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.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @SunMesa-7088
Ok, feel free to let me know, if there is any updates.

0 Votes 0 ·