0

I have a conda environment and I would like to run a jupyter console within that environment. I do the usual source activate myenv and then jupyter console. The source activate myenv works since which python points to the right path. However, it doesn't seem like the jupyter console is picking up the right env. I have done this:

python -m ipykernel install --user --name myenv --display-name "Python (myenv)"

I have nb_conda and nb_conda_kernels installed. What's going on with my setup? I have Ipython 6.4.0, Python 3.6.5. Also, coming from an R background, I find it weird that I'm spending so much time on the basic setup where in R things just work. Is there something I'm missing or doing wrong? How can i check which env Ipython is running in?

Alex
  • 1,281
  • 1
  • 13
  • 26

2 Answers2

3

sys.executable does indeed give you that info. This is how one should actually start the correct kernel (provided by How to start an ipython shell(not notebook) within a conda or virtualenv):

source activate myenv
python -m ipykernel install --user --name myenv --display-name "myenv"
jupyter console --kernel myenv

To get a list of the kernels that can be used:

jupyter kernelspec list
Alex
  • 1,281
  • 1
  • 13
  • 26
1
import sys
print(sys.executable)

Your conda environment is just a unique interpreter executable with its own PATH, etc. You could then regex on the string of its location to get the environment name.

tykom
  • 659
  • 5
  • 8
  • The value of ``sys.prefix`` would usually give the root directory of a virtual environment. If conda env are similar you just need to take last segment of path from that. Possibly easier to determine from that than ``sys.executable``. – Graham Dumpleton Jul 26 '18 at 23:33
  • Indeed, either will work. `sys.prefix.split('\')[-1]` or `sys.executable.split('\')[-2]` should return the name of your `env`. – tykom Jul 26 '18 at 23:37
  • yes, `sys.executable` is pointing to the wrong interpreter.. how can i change that? so i just manually set sys.executable? – Alex Jul 27 '18 at 01:07