1

I am using orgmode and Babel to run Python source code blocks. I noticed that if I define a function that returns a value, and if this function has a for loop inside it, then evaluating it with Babel leads to an IdentationError: unexpected indent.

For example, in org-mode write the following:

#+BEGIN_SRC python :results silent :session pysession
  def foo():
      for _ in range(1):
          pass
      return 1

  print(foo())
#+END_SRC

Then calling C-c C-c sends the code to the pysession buffer running the Python session, and it results in the error IdentationError: unexpected indent.

However, if we remove the for loop from the function definition, then things just work (prints 1):

#+BEGIN_SRC python :results silent :session pysession
  def foo():
      return 1

  print(foo())
#+END_SRC

Question: How to fix this behavior so that I can define functions with loops and have them work with babel?

Edit 1: The print can be removed from the code and the error still arises. The :results silent option is necessary since I do not want results in my org-file.

Edit 2: I noticed that when :results silent is selected and C-c C-c, it appears that Babel is sending the code line by line to the session, but when the line that contains the pass is send, Babel for some reason sends another RET, which makes the python REPL finish defining the function foo without any return statement. Then, when the line containing return 1 is sent, it is evaluated out of the scope of the function and causes the indentation error!!!

At this doc page it says:

Session mode in python is slightly different from non-session mode ... blank lines are special: they indicate the end of an indented block

But in this case it is not working at all: there is no line between pass and return 1, but Babel is treating as if there was, since it is thinking the indented block is ended after pass. It seems there is some bug with the backend.

When we use :results output, Babel actually saves the contents of the code block as is in a temporary file. It then reads the file and evaluates it in Python with exec. Since Python gets the entire code at once it works.

Thanks for helping!

  • 1
    An odd thing is that if :results silent is changed to :results output or :results value, the indentation error is not thrown. – Win Dec 23 '18 at 2:33
  • @Win I just tested it here and it does work. Unfortunately I have no idea how the code is sent to the python interpreter under the hood. – Guilherme Salomé Dec 24 '18 at 13:28
  • I just tried your code block and it seems that if you remove the 2 spaces at the beginning of each code line it works without throwing an indentation error, with :results output or with :results silent. – Lgen Jan 22 at 14:20
  • @Lgen even by removing the 2 spaces at the beginning (keeping the for loop in and :results silent) I still get the error. – Guilherme Salomé Feb 13 at 21:52
  • Do you have some other errors/messages in pysession buffer? Seems that the variable _ is not generated, and in your case is shadowed by the IndentationError: - in my configuration, when using :results silent I get the message NameError: '_' is not defined. – Ian Feb 15 at 14:22
0

For best results and features edit code blocks by running in a dedicated buffer using org-edit-special.

With pointer in the code block type ctrl-c' org-edit-src-code that will open in your case a python buffer with your hooks, the configured indentations and all.

Use ctrl-c' to return to the org file.

from gnu.org:

C-c ' for editing the current code block. It opens a new major-mode edit buffer containing the body of the ‘src’ code block, ready for any edits. C-c ' again to close the buffer and return to the Org buffer.

C-x C-s saves the buffer and updates the contents of the Org buffer.

Set org-edit-src-auto-save-idle-delay to save the base buffer after a certain idle delay time.

Set org-edit-src-turn-on-auto-save to auto-save this buffer into a separate file using auto-save-mode.

C-c ' to close the major-mode buffer and return back to the Org buffer.

While editing the source code in the major-mode, the org-src-mode minor mode remains active. It provides these customization variables as described below. For even more variables, look in the customization group org-edit-structure.

For more details and how to configure specifically editing-source-code

  • Thanks for your reply, but unfortunately that's what I already do to edit source-code blocks and the problem still happens. I also tried changing the default indentation but it also leads to the same problem. – Guilherme Salomé Dec 18 '18 at 21:10
0

I cannot reproduce your error. On my system, the answer is nil. But if you change the header to :results output :session pyssesion the result of evaluating is OK. See this for further explanations.

enter image description here

Of coarse, some other configurations/settings may be needed if the error will be the same.

  • The issue is that I do not want results on my org file. For example, if I just define the foo function without the print command, then I still get the same error. – Guilherme Salomé Dec 21 '18 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.