0

This question is similar to this question. I have to support Python 2.7 and 3.

I have some code that is written in python 3. This works great on my python 3 install (anaconda for win7 + jupyer-notebook). I need to unit test my code under a python 2.7 environment.

Is there an easy way to setup a 2.7 environment on my anaconda setup without clobbering my working install? It would be really cool to be able to run python 2 under a 3 notebook using a magic command!

name goes here
  • 280
  • 4
  • 10
  • 1
    you can have python 2 and 3 installed at the same time, their packages are managed separately – AllenMoh Aug 23 '17 at 19:00
  • 1
    You can definitely have both python 2 and python 3 environments set up under Anaconda, but a _notebook_ (Jupyter) only currently attaches to a single kernel, and thus can only run a single version at a time. If you can edit the Python 2 code, use `six` as suggested in the question you linked to so that your code runs in a single Py 2 or Py 3 environment. What are you actually trying to accomplish? Please give example. – scnerd Aug 23 '17 at 19:04
  • thanks for the tips! I maintain this package: https://github.com/twdb/sonde3 I have an example notebook, and unit tests (nosetests). My unit tests work fine in python3, but I need to run this in a 2.7 environment to bug fix and run the tests. – name goes here Aug 23 '17 at 19:26

4 Answers4

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

Paras jain
  • 432
  • 3
  • 15
0

Yes, you can have python 2.7 and 3 on the same system. Use the shebang on the top of your script to make the distinction between the 2 as necessary. These discussions should also address some of your concerns -

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?.

0

What you can do is creating 2 conda virtual environment and choose the kernel you want your jupyter notebook to run on. You should install nb_conda_kernels so that your environments are automatically recognized as different kernels. Follow this procedure (after having installed anaconda/miniconda):

  • Outside any environment, install nb_conda_kernels: conda install -c conda-forge nb_conda_kernels
  • Outside any environment, install jupyter: pip install jupyter
  • Create a virtual environment and activate it
  • In the environment, install jupyter: pip install jupyter
  • Inside any environment, run jupyter notebook
  • Choose the environment by clicking on the kernels tab

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

Mederic Fourmy
  • 321
  • 1
  • 2
  • 11
0

The following is the steps to create a Anaconda virtual environment to ease your testing.

  1. Open a Anaconda Prompt window.

  2. Type conda create -n py27 python=2.7 anaconda to create a new virtual environment named py27. You can create any new virtual environment with different version of Python, e.g. Python=3.6, for testing purpose. enter image description here

  3. Launch the root Jupyter Notebook to start Jupyter server.
    enter image description here

  4. Open your Python 3 notebook with the codes to be tested, from your Jupyter dashboard browser.

  5. From menu bar, → Kernel → Change kernel → {select target env} for your code testing
    enter image description here

Hope this help.

thewaywewere
  • 8,128
  • 11
  • 41
  • 46