7

I've recently moved to a new computer and cannot get org babel to work with Python (it worked fine on my old machine). I'm using OS X 10.12.6 with Emacs for Mac OS X based on Emacs 25.3 with org-mode-9.1.12 and Anaconda3 as my Python distribution.

The problem is that whenever I try and evaluate a code block I get the message

Warning (python): Your ‘python-shell-interpreter’ doesn’t seem to support readline, yet ‘python-shell-completion-native-enable’ was t and "python" is not part of the ‘python-shell-completion-native-disabled-interpreters’ list. Native completions have been disabled locally."

In my init.el file I load exec-path-from-shell (because I'm using a Mac and not calling Emacs from the command line). My shell (bash) has the path to my Python distribution, anaconda3, which is in my home directory (because of new computer security rules at my place of work prevents a system wide installation). Python works fine from the command line (the commands python and python3 are symbolic links to the python executable) and from Jupyter notebooks, but not from within an emacs org file. My Python distribution has the readline module and it loads fine from the terminal.

I set up org-babel using

(org-babel-do-load-languages
  'org-babel-load-languages
    '((python . t)
      (matlab . t)))

This used to all work on my older system (before the new security rules, which might be a red-herring). My guess is that somehow org is picking up the wrong python but I can't figure out how or why.

Any thoughts on this would be greatly appreciated.

1
  • I recently had the same problem (under GNU/Linux) and it was solved by adding (ipython . t) to the list of 'org-babel-load-languages (even with ipython not installed).
    – Lgen
    May 1, 2018 at 17:52

1 Answer 1

8

Ah. It's entirely possible python is actually working; depending on exactly how you've set up your python src blocks in Org, you may simply not be getting any results. You may know this already, but just to cover bases: the default Org python src block behavior is that your result value is the last thing returned from your src block. Thus, this doesn't work:

#+BEGIN_SRC python
print(4)
#+END_SRC

#+RESULTS:
: None

You can either use return (because Org implicitly is wrapping your code in a function):

#+BEGIN_SRC python
return 4
#+END_SRC

#+RESULTS:
: 4

Or you can use the :results output header arg:

#+BEGIN_SRC python :results output
print(4)
#+END_SRC

#+RESULTS:
: 4

You might want to make sure the value of python-shell-interpreter points to the python you're expecting. (You can do this with C-h v python-shell-interpreter.) If it's wrong, you can change it with (setq python-shell-interpreter "PATH TO YOUR REAL PYTHON").

All this done, you should be able to silence that error message one of two ways:

  1. Be sure readline is installed on your system. (I'm not familiar with anaconda python distributions specifically, but this works for me with my regular python3 installation)
  2. In your init file, disable native completion with (setq python-shell-completion-native-enable nil), or by manually calling the command M-x python-completion-native-toggle.
0

Your Answer

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

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