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):
      return 1


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


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

Edit: 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.

Thanks for helping!

bumped to the homepage by Community yesterday

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

  • 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 18 hours ago

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

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 acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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