scimax icon indicating copy to clipboard operation
scimax copied to clipboard

How to use Python 3 by default?

Open DiogoFerrari opened this issue 5 years ago • 8 comments

DiogoFerrari avatar Jul 26 '19 20:07 DiogoFerrari

Maybe what you can do is to create a pyenv for python 3. You can do it using the command pyvenv-create inside emacs.

domingoUnican avatar Jul 26 '19 20:07 domingoUnican

That does not work. I use a venv with python 3.6 and the only jupyter kernel for that ve is python3. But the ipython code blocks use python 2.7. (I am using Ubuntu and emacs 26)

DiogoFerrari avatar Jul 26 '19 20:07 DiogoFerrari

This would by my approaches, in order of preference.

  1. Install python3 so it is the system default.
  2. Customize ob-ipython-command to launch a python3 version of ipython
  3. Customize org-babel-python-command to launch the python3 of your choice
  4. this doesnt' make it default, but you can use :python /path/to/python3 on a per python block basis I think, and probably set it as a generic header in the file..

jkitchin avatar Jul 26 '19 20:07 jkitchin

The only missing piece was the ob-ipython-command. Now it is working fine. Thank you!

An unrelated topic: I noticed a JSON error in the upstream version of ob-ipython see here caused by an unexpected error message in the shell. Maybe it will come up here, so it may be good to be aware of.

DiogoFerrari avatar Jul 26 '19 21:07 DiogoFerrari

... and as I predicted, the JSON error occurred. The problem is that any warning that happens in the shell command produces a JSON readtable error in the .org file. It happens when using #+INCLUDE:, for instance.

Setting ob-ipython-command to ipython3 does not help because it causes some warnings in the shell (with python 3.6.1), which results in JSON readtable error.

The solution I found was to let ob-ipython-comman to jupyter to use the jupyter console, but modify this function below to include the argument "--kernel=python3". It is a provisory solution, but make it possible to use ob-ipython (upstream version) in combination with your enhancements.

(defun ob-ipython--kernel-repl-cmd (name)
  (list ob-ipython-command "console" "--simple-prompt" "--existing" "--kernel=python3"
        (ob-ipython--kernel-file name)))

DiogoFerrari avatar Jul 26 '19 21:07 DiogoFerrari

Can you give me mwe org file that causes the json warning? I never see this issue my self.

I saw your issue on ob-ipython. I am not sure what the best solution here is. It sounds like you have some shell interaction causing the problem. I don't see that here. I will be out for the next week, when I get back I will see if it seems to be a windows/linux issue.

jkitchin avatar Jul 26 '19 21:07 jkitchin

Here is a mwe:

#+TITLE:  title

#+INCLUDE: tmp2.org

* A section
#+BEGIN_SRC ipython :session tmp :exports both :results output
print("ok")
#+END_SRC

I am using

  • Org mode version 9.0.9
  • Ubuntu 18.04.2
  • GNU Emacs 26.2 (build 2, x86_64-pc-linux-gnu, GTK+ Version 3.22.30)
  • Python 3.6.8
  • IPython 7.5.0
  • Jupyter console 5.2.0
  • ob-ipython here
  • plus the two .el files you (@jkitchin) mention here

DiogoFerrari avatar Jul 27 '19 04:07 DiogoFerrari

I have good experiences on scimax using anaconda python (rather than the system's python) and pyvenv on both Ubuntu and Fedora. I put this code in my user.el near the top:

;; pyvenv setup
(require 'pyvenv)
(setenv "WORKON_HOME" "~/anaconda/envs/")
(setq pyvenv-workon "python36")

roblem avatar Jul 27 '19 19:07 roblem