jupyter notebook running kernel in different env

前端 未结 5 1739
醉酒成梦
醉酒成梦 2020-11-28 01:11

I\'ve gotten myself into some kind of horrible virtualenv mess. Help?!

I manage environments with conda. Until recently, I only had a python2 jupyter

相关标签:
5条回答
  • 2020-11-28 01:43

    There is also an easy way here

    workon my-virtualenv-name  # activate your virtualenv, if you haven't already
    pip install tornado==4.5.3
    pip install ipykernel==4.8.2
    

    You should now be able to see your kernel in the IPython notebook menu: Kernel -> Change kernel and be able to switch to it (you may need to refresh the page before it appears in the list). IPython will remember which kernel to use for that notebook from then on.

    This worked for me. source

    0 讨论(0)
  • 2020-11-28 01:48

    In my case somehow jupyter wasn't able to 'pick' the virtual environment's python. So I had to edit ~/.local/share/jupyter/kernels/{my_env_name}/kernel.json and add path to the interpreter

    in the argv key.

    0 讨论(0)
  • 2020-11-28 01:50
    pip install --user ipykernel
    python -m ipykernel install --user --name=myenv
    
    Output
    Installed kernelspec myenv in /home/user/.local/share/jupyter/kernels/myenv
    

    and go to above directory open kernel.json

    {
     "argv": [
      "/home/user/anaconda3/envs/myenv/bin/python", # path to your virtual environment python
      "-m",
      "ipykernel_launcher",
      "-f",
      "{connection_file}"
     ],
     "display_name": "myenv",
     "language": "python"
    }
    
    0 讨论(0)
  • 2020-11-28 02:02

    This is a tricky part of ipython / Jupyter. The set of kernels available are independent of what your virtualenv is when you start jupyter Notebook. The trick is setting up the the ipykernel package in the environment you want to identify itself uniquely to jupyter. From docs on multiple ipykernels,

    source activate ENVNAME
    pip install ipykernel
    python -m ipykernel install --user --name ENVNAME --display-name "Python (whatever you want to call it)"
    

    If you only want to have a single Python 3 kernel, from the conda environment, just use python -m ipykernel install --user and it will reset the default python to the one in the virtualenv.

    And yes, you will need to restart the kernel and re-run the prior steps.

    See Also Using both Python 2.x and Python 3.x in IPython Notebook

    0 讨论(0)
  • 2020-11-28 02:02

    @tschundler's solution works perfectly if your environment has already been created.

    If you want to change the default kernel at the creation of your virtual environment and avoid any manual configuration, you just need to add jupyter at the end of the conda command:

    conda create --name ENVNAME python=PYTHONVERSION jupyter

    The correct kernel will then be used when you use ipython or jupyter notebook.

    0 讨论(0)
提交回复
热议问题