2

I have the following settings on for org-babel

#+PROPERTY: header-args:python :python ~/env/pt/bin/python :session a :results drawer output replace

When I evaluate a code block, the results are some long command string

#+begin_src python
  print(2)
#+end_src

#+RESULTS:
:results:
__PYTHON_EL_eval("try:\n    with open('/var/folders/rv/z9fg34554pv9zbn9grgcpv2m0000gn/T/babel-luDzsI/python-cj3oCx') as __org_babel_python_tmpfile:\n        exec(compile(__org_babel_python_tmpfile.read(), __org_babel_python_tmpfile.name, 'exec'))\nexcept:\n    raise\nfinally:\n    print('
:end:

If I go into the Python inferior, I find that indeed, the commands being sent to it are this weird stuff.

>>> 
__PYTHON_EL_eval("try:\n    with open('/var/folders/rv/z9fg34554pv9zbn9grgcpv2m0000gn/T/babel-luDzsI/python-yTGDdP') as __org_babel_python_tmpfile:\n        exec(compile(__org_babel_python_tmpfile.read(), __org_babel_python_tmpfile.name, 'exec'))\nexcept:\n    raise\nfinally:\n    print('org_babel_python_eoe')", "<string>")
2
org_babel_python_eoe
>>> 
__PYTHON_EL_eval("try:\n    with open('/var/folders/rv/z9fg34554pv9zbn9grgcpv2m0000gn/T/babel-luDzsI/python-cj3oCx') as __org_babel_python_tmpfile:\n        exec(compile(__org_babel_python_tmpfile.read(), __org_babel_python_tmpfile.name, 'exec'))\nexcept:\n    raise\nfinally:\n    print('org_babel_python_eoe')", "<string>")
2
org_babel_python_eoe

If I enter commands into the inferior directly it works fine (though it annoyingly echoes all the commands first).

If I don't use a session it works fine.

I have

  • Org mode version 9.6.15
  • Python 3.11.9
  • GNU Emacs 29.2 (build 1, aarch64-apple-darwin21.6.0, NS appkit-2113.60 Version 12.6.6 (Build 21G646)) of 2024-01-18
1
  • 1
    What version of Org mode are you using? What python version? I obviously don't have ~/env/pt/bin/python, but when I replace that with /usr/bin/python3, it all works for me. Org mode version 9.8-pre (release_9.7.6-81-gd6c0bb), python version 3.12.3.
    – NickD
    Commented Jul 7 at 16:09

1 Answer 1

3

The observed strange behavior is usual in MacOSX computers and is due to readline module not beeing easy accessible.

To explain more detailed:

Quoting from readline documentation:

Some platforms, such as Mac OS X, do not ship with GNU readline installed. The readline extension module in the standard library of Mac “system” Python uses NetBSD’s editline (libedit) library instead, which is a readline replacement with a less restrictive software license.

This assertion and the string /usr/local/bin/libedit.3.dylib checked for is the source for __PYTHON_EL_eval(... in org results or python repl.

The above quoted phrase is false today; the libedit library is missing in normal installation. Only if you install developer tools, you may find out a file libedit.tbd in Developer folder, but this cannot be moved to or installed (even with sudo) as /usr/lib/libedit.3.dylib or in /usr/local/bin due to safety restrictions imposed by Apple. (and temporarily disabling them is not a solution).

On the other part, starting with Python 3.11, a Mac readline library is built-in in Python; it can be found in /opt/homebrew/Cellar/[email protected]/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload/readline.cpython-311-darwin.so, or (depending on installation mode) e.g in Python 3.12 here /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/lib-dynload/readline.cpython-312-darwin.so. This readline is not recognized, and the python-mode version in Emacs 29.x still looks for libedit.

So no immediate solutions. Maybe using Linux or Jupyter notebook.

EDIT: I think there is a simple solution to solve your problem; according to this page, about a month ago was added an update for Python 3.11 and 3.12, for the package gnureadline. Do the following:

  • Check it you get this in your terminal:
$ python -c "import readline; print(readline.__doc__)"
Importing this module enables command line editing using libedit readline.
  • Do in terminal:
$ pip3 install gnureadline
  • The next step, in terminal:
$ python3 -m override_readline
  • Check if succes:
$ python3
Python 3.12.1 (v3.12.1:2305ca5144, Dec  7 2023, 17:23:38) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Using GNU readline instead of the default readline (see usercustomize.py)

After that restart your Emacs and check again source code block in your org file. I got this:

enter image description here

4
  • 1
    _PYTHON_EL_eval and _PYTHON_EL_eval_file are Python functions defined in python.el whose definitions are injected into a comint-based python shell. For the definitions, see python.el:ll. 3527--3569; for the injection, see python.el:ll.3592--3595. What does readline have to do with it?
    – NickD
    Commented Jul 10 at 19:22
  • 1
    @NickD: this page explain better than me what the problem is with readline. About __PYTHON_EL_.... the inferior Python process `reads' the lines sent by Emacs; if not a good readline or some bugs what we know about? seems in this case _PYTHON_EL fails to correctly/fully send lines/file.
    – Ian
    Commented Jul 11 at 13:55
  • 1
    Following the steps described on that page and installing gnureadline followed by the command python3 -m override_readline result is good, the message is not anymore displayed and in org-mode the result of executing a python code block is now OK.
    – Ian
    Commented Jul 11 at 13:56
  • Thanks for the explanation and for adding the steps to a solution!
    – NickD
    Commented Jul 11 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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