4

I have two Anaconda installations on my computer. The first one is based on Python 2.7 and the other is based on Python 3.4. The default Python version is the 3.4 though. What is more, I can start Python 3.4 either by typing /home/eualin/.bin/anaconda3/bin/python or just python. I can do the same but for Python 2.7 by typing /home/eualin/.bin/anaconda2/bin/python. My problem is that I don't know how to install new libraries under certain environments (either under Python 2.7 or Python 3.4). For example, when I do pip install seaborn the library gets installed under Python 3.4 by default when in fact I want to install it under Python 2.7. Any ideas?

EDIT

This is what I am doing so far: the ~/.bashrc file contains the following two blocks, of which only one is enabled at any given time.

# added by Anaconda 2.1.0 installer
export PATH="/home/eualin/.bin/anaconda2/bin:$PATH"

# added by Anaconda3 2.1.0 installer
#export PATH="/home/eualin/.bin/anaconda3/bin:$PATH"

Depending of which version I want to work, I open the fie, comment the opposite block and do source ~/.bashrc Then, I install the libraries I want to use one by one. But, is this the recommended way?

user706838
  • 5,132
  • 14
  • 54
  • 78
  • 2
    You'll most likely want a [virtualenv](https://virtualenv.pypa.io/en/latest/) :D (see [this question](http://stackoverflow.com/questions/1534210/use-different-python-version-with-virtualenv) for Python 2 / Python 3 differences in virtualenv). – Dettorer Dec 30 '14 at 16:24
  • what about using a virtual environment as http://docs.python-guide.org/en/latest/dev/virtualenvs/ to have complete control to libraries installation ? – aberna Dec 30 '14 at 16:24
  • can you please provide a more concrete solution? I have no previous experience with virtual environments... – user706838 Dec 30 '14 at 16:30
  • 2
    creating virtualenv instead of `conda`'s environments does not make much sense. Instead use the `conda` command to create new conda environments. You may be interested in reading `conda`'s docs http://conda.pydata.org/docs/ – cel Dec 30 '14 at 17:10
  • https://twitter.com/asmeurer/status/535842208616677376 – asmeurer Jan 26 '15 at 23:12

3 Answers3

9

You don't need multiple anaconda distributions for different python versions. I would suggest keeping only one.

conda basically lets you create environments for your different needs.

conda create -n myenv python=3.3 creates a new environment named myenv, which works with a python3.3 interpreter.

source activate myenv switches to the newly created environment. This basically sets the PATH such that pip, conda, python and other binaries point to the correct environment and interpreter.

conda install pip is the first thing you may want to do. Afterwards you can use pip and conda to install the packages you need.

After activating your environment pip install <mypackage> will point to the right version of pip so no need to worry too much.

You may want to create environments for different python versions or different sets of packages. Of course you can easily switch between those environments using source activate <environment name>.

For more examples and details you may want to have a look at the docs.

cel
  • 30,017
  • 18
  • 97
  • 117
0

Virtualenv seems like the obvious answer here, but I do want to suggest an alternative that we've been using to great effect lately: Fig - this is particularly effective since we use Docker in production as well, but I imagine that using Fig as a replacement for virtualenv would be quite effective regardless of your production environment.

Quentin Donnellan
  • 2,687
  • 1
  • 18
  • 24
-1

Using virtualenv is your best option as @Dettorer has mentioned.

I found this method of installing and using virtualenv the most useful. Check it out:

Proper way to install virtualenv

Community
  • 1
  • 1
Vijay
  • 924
  • 7
  • 14
  • 1
    As I tried to point out in the comments, I think using `virtualenv`s do not make sense when using `anaconda`. `conda`'s environments are based on virtual envs, but are much more powerful for various reasons. – cel Dec 30 '14 at 23:15