Python Anaconda usage summary

Python Anaconda usage summary

Install Anaconda

For Mac and Linux systems, after Anaconda is installed, it is actually just another folder ( / anaconda) in the main directory , and Windows will write to the registry. During installation, the installation program will add the bin directory to the PATH (Linux / Mac writes /.bashrc, Windows adds to the system variable PATH), these operations can also be completed by themselves. Taking Linux / Mac as an example, the operation to set PATH after installation is

# 将anaconda的bin目录加入PATH,根据版本不同,也可能是~/anaconda3/bin
echo 'export PATH="~/anaconda2/bin:$PATH"' >> ~/.bashrc
# 更新bashrc以立即生效
source ~/.bashrc

After configuring the PATH, you can use the which condaor conda --versioncommand to check whether it is correct.

If you install the corresponding version of Python 2.7, run python --versionor python -Vyou can get

Python 2.7.12 :: Anaconda 4.1.1 (64-bit)

Explain that the default environment of this release is Python 2.7.

Conda's Environmental Management

Conda's environment management function allows us to install several different versions of Python at the same time , and can switch freely.

Suppose we need to install Python 3.4, at this time, we need to do the following:

# 创建一个名为python34的环境,指定Python版本是3.4(不用管是3.4.x,conda会为我们自动寻找3.4.x中的最新版本)
conda create --name python34 python=3.4

# 安装好后,使用activate激活某个环境
activate python34 # for Windows
source activate python34 # for Linux & Mac
# 激活后,会发现terminal输入的地方多了python34的字样,实际上,此时系统做的事情就是把默认2.7环境从PATH中去除,再把3.4对应的命令加入PATH

# 此时,再次输入
python --version
# 可以得到`Python 3.4.5 :: Anaconda 4.1.1 (64-bit)`,即系统已经切换到了3.4的环境

# 如果想返回默认的python 2.7环境,运行
deactivate python34 # for Windows
source deactivate python34 # for Linux & Mac

# 删除一个已有的环境
conda remove --name python34 --all

Different python environments installed by users will be placed in the directory ~/anaconda/envs, you can run in the command to conda info -eview the installed environment, the currently activated environment will be displayed with an asterisk or parentheses.

Conda's package management

Conda's package management is easier to understand, this part of the function is similar to pip.

For example, if you need to install scipy:

# 安装scipy
conda install scipy
# conda会从从远程搜索scipy的相关信息和依赖项目,对于python 3.4,conda会同时安装numpy和mkl(运算加速的库)

# 查看已经安装的packages
conda list
# 最新版的conda是从site-packages文件夹中搜索已经安装的包,不依赖于pip,因此可以显示出通过各种方式安装的包

Some common operations of conda are as follows:

# 查看当前环境下已安装的包
conda list

# 查看某个指定环境的已安装包
conda list -n python34

# 查找package信息
conda search numpy

# 安装package
conda install -n python34 numpy
# 如果不用-n指定环境名称,则被安装在当前活跃环境
# 也可以通过-c指定通过某个channel安装

# 更新package
conda update -n python34 numpy

# 删除package
conda remove -n python34 numpy

As mentioned earlier, conda treats conda, python, etc. as packages, so you can use conda to manage conda and python versions, for example:

# 更新conda,保持conda最新
conda update conda

# 更新anaconda
conda update anaconda

# 更新python
conda update python

# 假设当前环境是python 3.4, conda会将python升级为3.4.x系列的当前最新版本

Added: If you create a new python environment, such as 3.4, run

conda create -n python34 python=3.4

After that, conda only installs the necessary items related to python 3.4, such as python, pip, etc. If you want the environment to be like the default environment, install the anaconda collection package, you only need:

# 在当前环境下安装anaconda包集合
conda install anaconda

# 结合创建环境的命令,以上操作可以合并为
conda create -n python34 python=3.4 anaconda
# 也可以不用全部安装,根据需求安装自己需要的package即可

Set up a domestic mirror

If you need to install a lot of packages, you will find that the download speed of conda is often very slow, because the server of Anaconda.org is abroad. Fortunately, the Tsinghua TUNA mirror source has the mirror of Anaconda warehouse, we can add it to the configuration of conda:

# 添加Anaconda的TUNA镜像
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
# TUNA的help中镜像地址加有引号,需要去掉

# 设置搜索时显示通道地址
conda config --set show_channel_urls yes

After executing the above command, it will generate ~ / .condarc (Linux / Mac) or C: \ Users \ USER_NAME.condarc file, which records our configuration of conda, and directly create and edit the file manually is the same effect.

to sum up

Anaconda has the characteristics of cross-platform, package management, and environment management, so it is very suitable for quickly deploying Python environment on new machines. In summary, the entire installation and configuration process is as follows:

  1. Download Anaconda and install.
  2. Configure PATH (bashrc or environment variable), change Tsinghua TUNA mirror source.
  3. Create the required Python environment without a version.
Published 420 original articles · 143 thumbs up · 890,000 views

Guess you like

Origin blog.csdn.net/jeikerxiao/article/details/97243432