I am not sure it makes sense to change anything for this. There is different behavior with scripts and the interpreter independently of org-mode, e.g. with python -i:
>>> for i in range(3): ... print(i) ... i File "<stdin>", line 3 i ^ SyntaxError: invalid syntax Vanilla python sessions are kind of maddening. These seemingly identical blocks are different, i.e. one works and one doesn't! Spoiler alert, they are not identical. #+BEGIN_SRC python :results output org drawer :session for i in range(3): for j in range(3): pass print(i) #+END_SRC #+RESULTS: :RESULTS: ... File "<stdin>", line 3 ^ IndentationError: expected an indented block File "<stdin>", line 1 pass ^ IndentationError: unexpected indent >>> File "<stdin>", line 1 print(i) ^ IndentationError: unexpected indent :END: This block which works has one space at the beginning of the blank lines. #+BEGIN_SRC python :results output org drawer :session for i in range(3): for j in range(3): pass print(i) #+END_SRC #+RESULTS: :RESULTS: ... ... ... ... ... 0 1 2 :END: This kind of error would be hard to reliably fix IMHO since it would rely on replacing blank lines with at least a space, and adding a blank line after indentation changes, except they can not be empty, they need at least a space in them. It is not clear that is a good idea. Maybe a test that replaces "\n" with "\n \n" might clear it up, but might also add a bunch of the ... >>> characters in the output? This is not an issue with python scripts or ipython, however. Nicolas Goaziou writes: > Hello, > > William Henney <when...@gmail.com> writes: > >> I can reproduce your problem. This is (arguably) a bug in ob-python when >> using the vanilla python interpreter together with the :session argument. >> You can work around it by putting a blank line after the for-loop in your >> second code block. >> >> I say that it is arguable that this is a bug or not since you would have >> exactly the same error if you were to literally type your code block in at >> the python interactive prompt. That is, you have to give a second newline >> in order to close the loop and return to the top-level prompt. However, it >> is admittedly confusing to have different behavior with and without the >> ":session" argument. > > Thank you for the analysis. Would you have a suggestion on how to > improve the situation? > > Regards, -- Professor John Kitchin Doherty Hall A207F Department of Chemical Engineering Carnegie Mellon University Pittsburgh, PA 15213 412-268-7803 @johnkitchin http://kitchingroup.cheme.cmu.edu