Mac下Org-mode Python代码块输出异常问题求助
Org-mode Python代码块输出异常解决(Mac环境)
问题现象
从Windows切换至Mac后,在Emacs 29.4(macOS Sonoma 14.4)中运行Org-mode的Python代码块时出现以下异常:
- Org-mode缓冲区显示截断的后端调用语句
__PYTHON_EL_eval(...) - 真实Python输出无法在Org-mode缓冲区展示,但代码实际执行正常,结果可在交互式Python会话缓冲区查看
示例代码与异常输出
示例代码
#+BEGIN_SRC python :python python3 :session py :results output :noweb yes :tangle no print("Somehow the output of these source blocks are not displayed correctly? I did not have this issue with emacs on Windows...") #+END_SRC
异常输出
Org-mode缓冲区的结果区域:
: __PYTHON_EL_eval("try: with open('/var/folders/ty/x6hl83sn5cz667zpq3yk02lr0000gn/T/babel-PJfJby/python-HFmx85') as __org_babel_python_tmpfile: exec(compile(__org_babel_python_tmpfile.read(), __org_babel_python_tmpfile.name, 'exec')) except: raise finally: print('
交互式Python会话缓冲区显示内容:
__PYTHON_EL_eval_file("/var/folders/ty/x6hl83sn5cz667zpq3yk02lr0000gn/T/py66QmoC", "/var/folders/ty/x6hl83sn5cz667zpq3yk02lr0000gn/T/py66QmoC", True) Somehow the output of these source blocks are not displayed correctly? I did not have this issue with emacs on Windows... org_babel_python_eoe >>>
曾尝试设置:results value,仅返回"None"且不显示后端字符串,但未解决核心问题。
解决方法
在Emacs配置文件中添加以下设置,强制Python开启行缓冲,避免输出缓冲导致的截断和显示异常:
(setq org-babel-python-command "python3 -c \"import sys; sys.stdout.reconfigure(line_buffering=True); exec(open('%s').read())\"")
若使用Python 2(不推荐),可改用:
(setq org-babel-python-command "python -u -c \"exec(open('%s').read())\"")
内容的提问来源于stack exchange,提问作者Chris
相关产品推荐
相关产品推荐

