179

I'm relatively new in macOS. I've just installed XCode (for c++ compiler) and Anaconda with the latest Python 3 (for myself). Now I'm wondering how to install properly second Anaconda (for work) with Python 2?

I need both versions to work with iPython and Spyder IDE. Ideal way is to have totally separate Python environments. For example, I wish I could write like conda install scikit-learn for Python 3 environment and something like conda2 install scikit-learn for Python 2.

unrealapex
  • 578
  • 9
  • 23
night_bat
  • 3,212
  • 5
  • 16
  • 19
  • 1
    The solution below seems to work well for me in Ubuntu as well. While Linux users should know well enough that a solution for Mac is worth trying on Linux as well, I recommend that you remove the osx and Mac tags from your title and replace with general "Unix". That way it will come up in more search results and/or not be ignored by Linux users. Especially if they are new to Linux or just don't know that Mac is Unix based. – RMurphy Jan 23 '17 at 02:20

3 Answers3

332

There is no need to install Anaconda again. Conda, the package manager for Anaconda, fully supports separated environments. The easiest way to create an environment for Python 2.7 is to do

conda create -n python2 python=2.7 anaconda

This will create an environment named python2 that contains the Python 2.7 version of Anaconda. You can activate this environment with

source activate python2

This will put that environment (typically ~/anaconda/envs/python2) in front in your PATH, so that when you type python at the terminal it will load the Python from that environment.

If you don't want all of Anaconda, you can replace anaconda in the command above with whatever packages you want. You can use conda to install packages in that environment later, either by using the -n python2 flag to conda, or by activating the environment.

asmeurer
  • 86,894
  • 26
  • 169
  • 240
  • 1
    Thanks! Can I run two Spider IDE's with IPythons on Python 2.7 and Python 3.4 simultaneously that way? – night_bat Jun 26 '14 at 08:28
  • I believe so. I'm quite certain, though, because I'm not sure how it will handle the user settings, which are stored outside of the environment. – asmeurer Jun 26 '14 at 19:50
  • Great! Everything works just fine: both IDE runs in parallel. However, I still have no idea how to run two IPythons in browser. – night_bat Jun 27 '14 at 09:03
  • Start one `ipython notebook` from one terminal, then open another one, activate the other environment, and start the other. – asmeurer Jun 27 '14 at 15:25
  • 2
    Each `ipython notebook` sets up another port (localhost:8888, localhost:8889, etc.). So just keep them straight and you're all set. – alexis May 01 '15 at 15:52
  • 5
    Then how can I switch between `python2` and `python3`? And BTW, I'm on `win7`. – ZK Zhao Jun 12 '15 at 10:27
  • 25
    On Windows don't use `source`. It's just `activate python2` and `deactivate`. – asmeurer Jun 15 '15 at 16:33
  • This may not be the proper forum, but: I've followed these steps with mostly successful operations. However two things strike me as odd: 1) opening an IPython nb with a Python2 kernel allows me to use Python 2 or 3. Second, Spyder will not use python2 at all. Even when I launch from python2, it will still run python3. – Ryan Erwin Mar 14 '16 at 22:22
  • Where is python3.4 installed? I am tryin ot set up both versions in pycharm and I can't find where 3.4 is. – Chris Jul 10 '16 at 18:32
  • @cmarti1138 the one you installed initially will be the anaconda directory, and the environments will be in `anaconda/envs`. – asmeurer Jul 11 '16 at 13:33
  • yes... there will be two kernel engines one for each.. at the time of jupyter notebook start, it will load both the kernels... so all you have to do is to select appropriate kernels – Manoj Kumar Sep 02 '16 at 15:27
  • best is to create separate environments for each.. such as .. one for 2.7 and another for 3.5 and then work from respective environment – Manoj Kumar Sep 02 '16 at 15:32
  • When I did like this, (python2) is shown in my ubuntu terminal. Like (python2) shyamkkhadka@hp $ . How can I remove that (python2) ? – Shyamkkhadka Mar 08 '17 at 14:32
  • 3
    @Shyamkkhadka that is there to show you that you've activated a conda environment. If you don't like it you can disable it with `conda config --set changeps1 false`. – asmeurer Mar 08 '17 at 18:37
  • note that using just `conda create -n python2 python=2.7` (as suggested on the website of anaconda) does not work well – user1993 May 17 '18 at 17:57
  • Hi @asmeurer. Thanks for your answer. Giving that as a fact, then what is the meaning of separating conda2 with conda3? What is the main difference – Scott Yang Sep 30 '19 at 06:55
32

Edit!: Please be sure that you should have both Python installed on your computer.

Maybe my answer is late for you but I can help someone who has the same problem!

You don't have to download both Anaconda.

If you are using Spyder and Jupyter in Anaconda environmen and,

If you have already Anaconda 2 type in Terminal:

    python3 -m pip install ipykernel

    python3 -m ipykernel install --user

If you have already Anaconda 3 then type in terminal:

    python2 -m pip install ipykernel

    python2 -m ipykernel install --user

Then before use Spyder you can choose Python environment like below! Sometimes only you can see root and your new Python environment, so root is your first anaconda environment!

Anaconda spyder Python 2.7 or 3.5

Also this is Jupyter. You can choose python version like this!

Jupyter Notebook

I hope it will help.

Axis
  • 2,066
  • 2
  • 21
  • 40
  • 4
    Anaconda would need to be setup and configured for Jupyter kernels or Spyder before the specific libraries could be used from those options, though. – OneCricketeer Feb 26 '17 at 23:31
4

This may be helpful if you have more than one python versions installed and dont know how to tell your ide's to use a specific version.

  1. Install anaconda. Latest version can be found here
  2. Open the navigator by typing anaconda-navigator in terminal
  3. Open environments. Click on create and then choose your python version in that.
  4. Now new environment will be created for your python version and you can install the IDE's(which are listed there) just by clicking install in that.
  5. Launch the IDE in your environment so that that IDE will use the specified version for that environment.

Hope it helps!!

David
  • 524
  • 1
  • 7
  • 24