2

I'm trying to use ein to edit a jupyter notebook. I used conda to install the 'turicreate' package in a virtual environment and activated it. When I run the regular browser based jupyter notebook command everything works. Only inside ein does it not work -- I can open the notebook, but when I try to import turicreate it complains it can't find the module.

First I tried:

(setq ein:jupyter-default-server-command (format "%s/anaconda3/bin/jupyter" (getenv "HOME")))

This got ein to open the notebook, but the import failed. I figured the conda env wasn't propagating so I opened a shell, activated the environment and did:

$ env | grep -i py
$ env | grep -i conda

To see if activating just set env vars. So then I tried:

(setenv "CONDA_DEFAULT_ENV" "virtual_environment_name")
(setenv "CONDA_PREFIX" (format "%s/anaconda3/envs/virtual_environment_name" (getenv "HOME")))

I shutdown all jupyter servers and tried ein:run again. Still no dice.

So then I wrote a shell script:

#!/bin/bash

conda activate virtual_environment_name
$HOME/anaconda3/bin/jupyter "$@"

And set:

(setq ein:jupyter-default-server-command (format "%s/jupyter-with-anaconda.sh" (getenv "HOME")))

And it STILL can't find the module. At this point I'm at a loss. I have no idea how the environment state gets to the browser based jupyter so I don't know how to duplicate it for ein.

Any ideas?

Joseph Garvin
  • 2,061
  • 8
  • 21

1 Answers1

2

My script didn't work for 2 reasons:

  • conda wasn't in my path
  • conda activate doesn't work in bash scripts out of the box because it relies on bash functions

Solution:

#!/bin/bash

set -e
set -o pipefail

# otherwise can't find conda
export PATH=$HOME/anaconda3/bin:$PATH

# either of these will make conda activate work inside a bash script
# https://stackoverflow.com/questions/34534513/calling-conda-source-activate-from-bash-script
# https://github.com/conda/conda/issues/7980
#source $(conda info --base)/etc/profile.d/conda.sh
eval "$(conda shell.bash hook)"

conda activate virtual_environment_name
jupyter "$@"
exit $?
Joseph Garvin
  • 2,061
  • 8
  • 21