Search code examples
python-3.xcompatibilityenvironmentjupyterconda

Activating a new Conda env through shell from within Jupyter Notebook


I'm working with a Jupyter Notebook written in Python 3, and I would like to run Python 2 scripts from within that Notebook. I was wondering if it's possible to run Shell commands from within the Notebook, and have these Shell commands running under a different environment.

For example, if env2 is a Conda environment that runs Python 2, and env3 runs Python 3, and my Jupyter Notebook runs in env3, maybe I could write within my Notebook: ! source activate env2 ! script_that_uses_python2.py and then continue with python 3 code that is in the Notebook (and uses the output of script_that_uses_python2.py).

I tried it and it didn't work (! conda info --envs showed that env3 is still running). Any advice on how to change the environment in the middle of the Notebook and then go back to the original environment?


Solution

  • As far as I know, you cannot activate another environment and have it work like that. What you can do is run that Python explicitly, something like

    !/path/to/anaconda/envs/python2env/bin/python script_that_uses_python2.py
    

    If I run

    !/path/to/anaconda/envs/python2env/bin/python -c "import sys; print sys.path"
    

    on my system, it only shows Python 2 directories, so it would probably find the correct imports. However, the variables from that script won't be available in your notebook. You could have the Python 2 file write out a Pickle file and try to read that, maybe...