如何在特定环境下安装Python库 [英] How to install Python libraries under specific environments

查看:585
本文介绍了如何在特定环境下安装Python库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的计算机上安装了两个Anaconda.第一个基于Python 2.7,另一个基于Python 3.4.不过,默认的Python版本是3.4.而且,我可以通过输入/home/eualin/.bin/anaconda3/bin/python 或仅 python 来启动Python 3.4.对于Python 2.7,我可以执行相同的操作,方法是输入/home/eualin/.bin/anaconda2/bin/python .我的问题是我不知道如何在某些环境下(在python 2.7或python 3.4下)安装新库.例如,当我进行pip install seaborn安装时,默认情况下该库在Python 3.4下安装,而实际上我想在Python 2.7下安装它.有什么想法吗?

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?

编辑

到目前为止,这是我正在做的事情:〜/.bashrc文件包含以下两个块,其中在任何给定时间仅启用一个.

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"

根据要使用的版本,打开文件,注释对面的块并执行source ~/.bashrc然后,我要逐一安装要使用的库.但是,这是推荐的方法吗?

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?

推荐答案

对于不同的python版本,您不需要多个anaconda发行版.我建议只保留一个.

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

conda基本上可以让您创建满足您不同需求的环境.

conda basically lets you create environments for your different needs.

conda create -n myenv python=3.3创建一个名为myenv的新环境,该环境可与python3.3解释器一起使用.

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

source activate myenv切换到新创建的环境.基本上,这将PATH设置为使pipcondapython和其他二进制文件指向正确的环境和解释器.

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是您可能想做的第一件事.之后,您可以使用pipconda安装所需的软件包.

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.

激活环境后,pip install <mypackage>将指向pip的正确版本,因此不必太担心.

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

您可能想为不同的python版本或不同的软件包集创建环境.当然,您可以使用source activate <environment name>轻松在这些环境之间切换.

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

有关更多示例和详细信息,您可能需要查看 docs .

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

这篇关于如何在特定环境下安装Python库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆