Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install Python libraries under specific environments

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?

like image 416
user706838 Avatar asked Feb 11 '23 06:02

user706838


1 Answers

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.

like image 138
cel Avatar answered Feb 16 '23 01:02

cel