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

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

    Use sudo pip3 install jupyter for installing jupyter for python3 and sudo pip install jupyter for installing jupyter notebook for python2. Then, you can call ipython kernel install command to enable both types of notebook to choose from in jupyter notebook.

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

    The idea here is to install multiple ipython kernels. Here are instructions for anaconda. If you are not using anaconda, I recently added instructions using pure virtualenvs.

    Anaconda >= 4.1.0

    Since version 4.1.0, anaconda includes a special package nb_conda_kernels that detects conda environments with notebook kernels and automatically registers them. This makes using a new python version as easy as creating new conda environments:

    conda create -n py27 python=2.7 ipykernel
    conda create -n py36 python=3.6 ipykernel
    

    After a restart of jupyter notebook, the new kernels are available over the graphical interface. Please note that new packages have to be explicitly installed into the new environments. The Managing environments section in conda's docs provides further information.

    Manually registering kernels

    Users who do not want to use nb_conda_kernels or still use older versions of anaconda can use the following steps to manually register ipython kernels.

    configure the python2.7 environment:

    conda create -n py27 python=2.7
    conda activate py27
    conda install notebook ipykernel
    ipython kernel install --user
    

    configure the python3.6 environment:

    conda create -n py36 python=3.6
    conda activate py36
    conda install notebook ipykernel
    ipython kernel install --user
    

    After that you should be able to choose between python2
    and python3 when creating a new notebook in the interface.

    Additionally you can pass the --name and --display-name options to ipython kernel install if you want to change the names of your kernels. See ipython kernel install --help for more informations.

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

    With a current version of the Notebook/Jupyter, you can create a Python3 kernel. After starting a new notebook application from the command line with Python 2 you should see an entry „Python 3“ in the dropdown menu „New“. This gives you a notebook that uses Python 3. So you can have two notebooks side-by-side with different Python versions.

    The Details

    1. Create this directory: mkdir -p ~/.ipython/kernels/python3
    2. Create this file ~/.ipython/kernels/python3/kernel.json with this content:

      {
          "display_name": "IPython (Python 3)", 
          "language": "python", 
          "argv": [
              "python3", 
              "-c", "from IPython.kernel.zmq.kernelapp import main; main()", 
              "-f", "{connection_file}"
          ], 
          "codemirror_mode": {
              "version": 2, 
              "name": "ipython"
          }
      }
      
    3. Restart the notebook server.

    4. Select „Python 3“ from the dropdown menu „New“
    5. Work with a Python 3 Notebook
    6. Select „Python 2“ from the dropdown menu „New“
    7. Work with a Python 2 Notebook
    0 讨论(0)
  • 2020-11-22 03:07
    • If you are running anaconda in virtual environment.
    • And when you create a new notebook but i's not showing to select the virtual environment kernel.
    • Then you have to set it into the ipykernel using the following command
    $ pip install --user ipykernel
    $ python -m ipykernel install --user --name=test2
    
    0 讨论(0)
  • 2020-11-22 03:08

    Following are the steps to add the python2 kernel to jupyter notebook::

    open a terminal and create a new python 2 environment: conda create -n py27 python=2.7

    activate the environment: Linux source activate py27 or windows activate py27

    install the kernel in the env: conda install notebook ipykernel

    install the kernel for outside the env: ipython kernel install --user

    close the env: source deactivate

    Although a late answer hope someone finds it useful :p

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

    I looked at this excellent info and then wondered, since

    1. i have python2, python3 and IPython all installed,
    2. i have PyCharm installed,
    3. PyCharm uses IPython for its Python Console,

    if PyCharm would use

    1. IPython-py2 when Menu>File>Settings>Project>Project Interpreter == py2 AND
    2. IPython-py3 when Menu>File>Settings>Project>Project Interpreter == py3

    ANSWER: Yes!

    P.S. i have Python Launcher for Windows installed as well.

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