emacs-jupyter / jupyter

An interface to communicate with Jupyter kernels.
GNU General Public License v3.0
934 stars 92 forks source link

How to configure emacs-jupyter before jupyter is accessible? #251

Open mankoff opened 4 years ago

mankoff commented 4 years ago

Is there some way to initialize emacs-jupyter before initializing a conda environment?

I'm using miniconda and my system is configured so that my default shell does not have an active conda environment. My default shell has access to the system Python which does not include jupyter. This is where I launch emacs, but when I do I get this error:

Error retrieving kernelspecs: (file-missing "Searching for program" "No such file or directory" "jupyter")

If I have a base conda environment with jupyter activated, then everything works, but I don't want this base environment.

Various project folders have conda environments, and when I enter them I want to start emacs-jupyter for each of those environments. This works, because I've put a .dir-locals.el in the various folders that contains: ((nil . ((eval conda-env-activate "foo")))) to activate the foo environment when I'm working in that folder.

Nathan-Furnal commented 4 years ago

I actually fixed it, at least in my opinion.

First, you need jupyter installed on your system, out of conda. So in the shell, just :

pip3 install jupyter

Then, add the environments you need as mentioned here and in #253.

Now with this in your init :


(use-package conda
  :defer t
  :init
  (setq conda-anaconda-home (expand-file-name "~/miniconda3")
    conda-env-home-directory (expand-file-name "~/miniconda3"))
  :config
  (conda-env-initialize-interactive-shells)
  (conda-env-initialize-eshell))

(use-package jupyter
  :defer t
  :init
  (setq org-babel-default-header-args:jupyter-python '((:async . "yes")
                                                       (:session . "py"))))

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

Now with this, once you type jupyter-run-repl you can pick any environment you liked, as you named them.

This setup also allows to precise which kernel to use in org, you have to use the name you gave them during the above steps or you can always precise a named kernel in your init.

#+begin_src jupyter-python :kernel myKernel
<your code>
#+end_src

Some text 

#+begin_src jupyter-python :kernel otherKernel
<your code>
#+end_src

Like this, you can use multiple kernels in your org files and you can select different kernels as well with the REPL.