可以在同一个笔记本中运行Python 2.7和Python 3吗?

时间:2017-08-23 18:57:44

标签: python python-2.7 python-3.x anaconda jupyter-notebook

此问题与this question类似。我必须支持Python 2.7和3.

我有一些用python 3编写的代码。这在我的python 3安装(anaconda for win7 + jupyer-notebook)上运行得很好。我需要在python 2.7环境下对我的代码进行单元测试。

有没有简单的方法在我的anaconda设置上设置2.7环境而不破坏我的工作安装?能够使用魔术命令在3笔记本下运行python 2真的很酷!

4 个答案:

答案 0 :(得分:1)

您可以做的是创建2个conda虚拟环境并选择您希望运行jupyter笔记本的内核。您应该安装nb_conda_kernels,以便您的环境自动被识别为不同的内核。按照此程序(安装anaconda / miniconda后):

  • 在任何环境之外,请安装nb_conda_kernels:conda install -c conda-forge nb_conda_kernels
  • 在任何环境之外,安装jupyter:pip install jupyter
  • 创建虚拟环境并将其激活
  • 在环境中,安装jupyter:pip install jupyter
  • 在任何环境中,运行jupyter notebook
  • 单击内核选项卡
  • 选择环境

https://github.com/Anaconda-Platform/nb_conda_kernels

答案 1 :(得分:1)

Step1: Download anaconda for both the version

Step 2: open .bashrc

Step 3: add the path to new anaconda you have installed for eg:

export PATH="/home/paras/anaconda3/bin:$PATH"

Step 4: Now there will be 2 export path one for python 2 and one for python 3.

Comment the one which you don't want

答案 2 :(得分:0)

是的,您可以在同一系统上安装python 2.7和3。使用脚本顶部的shebang根据需要区分2。这些讨论还应解决您的一些问题 -

How to install both Python 2.x and Python 3.x in Windows 7

How do I run python 2 and 3 in windows 7?

答案 3 :(得分:0)

以下是创建Anaconda虚拟环境以简化测试的步骤。

  1. 打开[A, B, C, D]窗口。

  2. 键入Anaconda Prompt以创建新的 名为conda create -n py27 python=2.7 anaconda的虚拟环境。您可以使用不同版本的Python创建任何新的虚拟环境,例如py27,用于测试目的。 enter image description here

  3. 启动根Python=3.6以启动Jupyter服务器 enter image description here

  4. 使用您的Jupyter仪表板浏览器打开包含要测试代码的Python 3笔记本。

  5. 从菜单栏,→内核→更改内核→Jupyter Notebook进行代码测试 enter image description here
  6. 希望得到这个帮助。