When I run the following code block from Emacs org-babel:

#+BEGIN_SRC python :session *Python* :results output

  print("Hello World")


This is what prints out in my *Python* session:

>>> print("Hello World")
Hello World

>>> 'org_babel_python_eoe'
>>> 'org_babel_python_eoe'

How can I get rid of all these extra prompts and org_babel_python_eoe lines?


This patch does it here:

--- ../../../emacs-20190711/lisp/org/ob-python.el   2019-07-09 16:46:01.274976504 +0200
+++ ob-python.el    2019-08-08 21:51:11.185799006 +0200
@@ -294,7 +294,9 @@
 If RESULT-TYPE equals `output' then return standard output as a
 string.  If RESULT-TYPE equals `value' then return the value of the
 last statement in BODY, as elisp."
-  (let* ((send-wait (lambda () (comint-send-input nil t) (sleep-for 0 5)))
+  (let* ((erg "")
+         (filter-re (concat python-shell-prompt-regexp "\\|" "'org_babel_python_eoe'\n" "\\|" "#.+"))
+         (send-wait (lambda () (comint-send-input nil t) (sleep-for 0 5)))
        (tmp-file pp)
@@ -322,16 +324,17 @@
                   (format org-babel-python--exec-tmpfile
-          (mapconcat
-       #'org-trim
-       (butlast
+          ;; (mapconcat
+               ;; #'org-trim
+               ;; (butlast
             (session org-babel-python-eoe-indicator t body)
           (funcall input-body body)
           (funcall send-wait) (funcall send-wait)
           (insert org-babel-python-eoe-indicator)
           (funcall send-wait))
-        2) "\n")))
+               ;; 2) "\n")
+               ))
              (let ((tmp-file (org-babel-temp-file "python-")))
@@ -344,6 +347,10 @@
                    (insert org-babel-python-eoe-indicator)
                    (funcall send-wait)))
                (org-babel-eval-read-file tmp-file))))))
+    (when (listp results)
+      (dolist (ele results)
+        (setq erg (concat erg (ignore-errors (replace-regexp-in-string  filter-re "" ele))))))
+    (setq results erg)
     (unless (string= (substring org-babel-python-eoe-indicator 1 -1) results)
       (org-babel-result-cond result-params

Edit after comment: If you want to apply this, please consider it's only tested WRT examples given here. Make a copy of the original file - maybe undoing is needed. Put the diff-code into some file my-diff.patch. Install the command "patch" if not existing on your system. Call inside the directory where ob-python.el resides: patch < PATH/TO/my-diff.patch. Recompile or remove ob-python.elc - which would be loaded instead of patched source otherwise.

How to run a patched Emacs? Beside the current case: Download Emacs with sources from some gnu-repo. Mention "elc" ".o" in .gitignore. Then do "git init" followed by "git add --all". Now all changes are easily undone by "git checkout FILE".

  • thanks for this! For those of us unfamiliar with the development workflow, can you include the steps for applying this patch in Emacs? – Adam Aug 9 at 18:27

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.