2

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?

RGS
  • 23
  • 4
  • 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... – darthbith Sep 14 '16 at 11:50
  • It works! Thanks. – RGS Sep 14 '16 at 20:20

2 Answers2

2

This worked for me:

! source /home/ubuntu/miniconda/etc/profile.d/conda.sh && conda activate envname && python run.py

Note: It only works if you run all commands in one line connecting them with &&

nkaenzig
  • 684
  • 7
  • 14
1

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...

darthbith
  • 18,484
  • 9
  • 60
  • 76