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

后端 未结 11 2151
生来不讨喜
生来不讨喜 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:14

    If you’re running Jupyter on Python 3, you can set up a Python 2 kernel like this:

    python2 -m pip install ipykernel
    
    python2 -m ipykernel install --user
    

    http://ipython.readthedocs.io/en/stable/install/kernel_install.html

    0 讨论(0)
  • 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 <pkg-name>
    deactivate
    

    or

    kernel3
    python -m pip install <pkg-name>
    deactivate
    
    0 讨论(0)
  • 2020-11-22 03:19

    From my Linux installation I did:

    sudo ipython2 kernelspec install-self

    And now my python 2 is back on the list.

    Reference:

    http://ipython.readthedocs.org/en/latest/install/kernel_install.html


    UPDATE:

    The method above is now deprecated and will be dropped in the future. The new method should be:

    sudo ipython2 kernel install

    0 讨论(0)
  • 2020-11-22 03:19

    Under Windows 7 I had anaconda and anaconda3 installed. I went into \Users\me\anaconda\Scripts and executed

    sudo .\ipython kernelspec install-self
    

    then I went into \Users\me\anaconda3\Scripts and executed

    sudo .\ipython kernel install
    

    (I got jupyter kernelspec install-self is DEPRECATED as of 4.0. You probably want 'ipython kernel install' to install the IPython kernelspec.)

    After starting jupyter notebook (in anaconda3) I got a neat dropdown menu in the upper right corner under "New" letting me choose between Python 2 odr Python 3 kernels.

    0 讨论(0)
  • 2020-11-22 03:23

    A solution is available that allows me to keep my MacPorts installation by configuring the Ipython kernelspec.

    Requirements:

    • MacPorts is installed in the usual /opt directory
    • python 2.7 is installed through macports
    • python 3.4 is installed through macports
    • Ipython is installed for python 2.7
    • Ipython is installed for python 3.4

    For python 2.x:

    $ cd /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin
    $ sudo ./ipython kernelspec install-self
    

    For python 3.x:

    $ cd /opt/local/Library/Frameworks/Python.framework/Versions/3.4/bin
    $ sudo ./ipython kernelspec install-self
    

    Now you can open an Ipython notebook and then choose a python 2.x or a python 3.x notebook.

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