0

I have been running the Jupyter notebooks with multiple conda environments (kernels). This was accomplished by installing nb_conda and nb_conda_kernels.

After installing both of the above packages and restarting the notebook server I seem to have access to both conda environments in the jupyter notebook. However, I could not confirm that the underlying shell has the correct environment. For example if I start a two notebook servers one with Python 2.7 and one with 3.6, I get expected answers for the python versions, but not for executed shell commands.

Python 2.7.13:

import sys

print(sys.version)

#succeed evidence for running py < 3 
import commands

commands.getoutput('which python')

output:

2.7.13 |Anaconda custom (64-bit)| (default, Dec 20 2016, 23:09:15) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]

'/path/python/anaconda3/envs/py36/bin/python'

In this case I would have expected which python to produce the version of python that is active inside the Python 2.7.13 environment. But I know the path returned is actually the python that is used in the Python 3.6 environment (see below)

Python 3.6:

import sys

print(sys.version)

import subprocess

subprocess.check_output(["which","python"])

output:

3.6.1 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:09:58) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]

b'/path/python/anaconda3/envs/py36/bin/python\n'

Also, in the Python 3.6 environment I had this failure, which makes sense because the commands module was eliminated in Python 3:

# fail (evidence for running py 3.6 env)

import commands

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call      last)
<ipython-input-2-9cbf0185e88f> in <module>()
      1 # fail (evidence for running py 3.6 env)
----> 2 import commands

ModuleNotFoundError: No module named 'commands'

So, in either case the output of which python gave the version of python from the environment in which I started the Jupyter notebook. This leads me to believe that while the Python environment is as expected, the shell environment is somehow inconsistent with the Python conda environment. Why is that true? Will it cause problems?

villaa
  • 1,043
  • 3
  • 14
  • 32

1 Answers1

0

which python only gives you the path of python in environment variable PATH. But not the current python's version. It is determined by which environment you launch your jupyter notebook.

For example, you can run /path/python/anaconda3/envs/py35/bin/python in raw terminal while activating source activate py36. In this case, your python's version is 3.5, but which python will still give you /path/python/anaconda3/envs/py36/bin/python. Or you can run /path/python/anaconda3/envs/py27/bin/python, then your python's version is 2.7 while which python will give you py36 again.

Sraw
  • 18,892
  • 11
  • 54
  • 87
  • in my shell environment the act of switching the `conda` environment via `source activate py27` changes the `PATH` such that an invocation of `python` will then give the correct binary. I understand that the other binaries still exist and can be executed explicitly. But for the notebook I would've thought that the **environment** is switched when the notebook is created/restarted, so that `PATH` should be consistent. Is that not the case? It could have implications for other code that relies on environmental variables that are changed by a `conda` environment switch. – villaa Feb 12 '18 at 01:35
  • No, that's no the case. `jupyter` won't modify `PATH` but just use different `ipkernel`. But will it really have implications as you say? `conda` only insert `envs/xx/bin` in front of `PATH`. As far as I can tell, it will only influence `python`. – Sraw Feb 12 '18 at 02:11