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

后端 未结 11 2149
生来不讨喜
生来不讨喜 2020-11-22 02:27

I use IPython notebooks and would like to be able to select to create a 2.x or 3.x python notebook in IPython.

I initially had Anaconda. With Anaconda a global envi

11条回答
  •  伪装坚强ぢ
    2020-11-22 03:16

    These instructions explain how to install a python2 and python3 kernel in separate virtual environments for non-anaconda users. If you are using anaconda, please find my other answer for a solution directly tailored to anaconda.

    I assume that you already have jupyter notebook installed.


    First make sure that you have a python2 and a python3 interpreter with pip available.

    On ubuntu you would install these by:

    sudo apt-get install python-dev python3-dev python-pip python3-pip
    

    Next prepare and register the kernel environments

    python -m pip install virtualenv --user
    
    # configure python2 kernel
    python -m virtualenv -p python2 ~/py2_kernel
    source ~/py2_kernel/bin/activate
    python -m pip install ipykernel
    ipython kernel install --name py2 --user
    deactivate
    
    # configure python3 kernel
    python -m virtualenv -p python3 ~/py3_kernel
    source ~/py3_kernel/bin/activate
    python -m pip install ipykernel
    ipython kernel install --name py3 --user
    deactivate
    

    To make things easier, you may want to add shell aliases for the activation command to your shell config file. Depending on the system and shell you use, this can be e.g. ~/.bashrc, ~/.bash_profile or ~/.zshrc

    alias kernel2='source ~/py2_kernel/bin/activate'
    alias kernel3='source ~/py3_kernel/bin/activate'
    

    After restarting your shell, you can now install new packages after activating the environment you want to use.

    kernel2
    python -m pip install 
    deactivate
    

    or

    kernel3
    python -m pip install 
    deactivate
    

提交回复
热议问题