Detailed explanation of Anaconda installation and use

Article directory

Introduction to Anaconda

Anaconda is a collection of Python scientific and technical packages. It integrates many useful scientific computing packages. It includes more than 180 scientific computing packages and their dependencies such as conda, Python, NumPy, and pytorch;

What libraries does anaconda include:

  • conda: conda is an open source package management and environment management tool in Anaconda (the package management function is similar to pip, and the environment management function is similar to virtualenv, pipenv);
  • ipython: is a Python interactive shell, which is much easier to use and more powerful than the default Python Shell. It supports syntax highlighting, auto-completion, code debugging, and has many useful built-in functions and functions;
  • NumPy: Scientific computing toolkit, perhaps the most commonly used is its N-dimensional array object. Others include some mature function libraries, toolkits for integrating C/C++ and Fortran codes, linear algebra, Fourier transform and random Number generating functions, etc.;
  • matplotlib: It is the most famous drawing library in Python. It provides a set of command APIs similar to matlab, which is very suitable for interactive drawing. It can also be easily used as a drawing control and embedded in GUI applications. Matplotlib can cooperate with Using ipython shell, it provides a drawing experience that is no less than that of Matlab;
  • pandas: developed based on NumPy and Matplotlib, mainly used for data analysis and data visualization;
  • pytorch: deep learning framework;
1. Anaconda installation

Because Anaconda comes with python, after installing anaconda, the system automatically installs python, so you need to uninstall the local python environment first; (Of course, you can also choose not to uninstall the locally installed python environment. If you do not uninstall, the anaconda python version must be the same as The existing local python version must be consistent, otherwise anaconda may fail to be installed);

1. Download
(1) Official website download address: https://www.anaconda.com/download
(2) Alibaba open source software mirror station: http://mirrors.aliyun.com/anaconda/archive/http://mirrors.aliyun.com/anaconda/archive/

My download: Anaconda3-2023.09-0-Windows-x86_64.exe
Insert image description here

2. Installation
(1) Double-click the installation package -> Next -> I Agree -> Select All Users:

Insert image description here

(2) Select the installation location. C drive is not recommended because the virtual machine needs to install many packages (here I installed it on C drive because my computer only has C drive):

Insert image description here

(3) You can select all of the following 3 items -> Install -> After a long wait, about 4 hours -> The installation is finally completed:

Insert image description here
After a long wait, about 4 hours
Insert image description here
next
Insert image description here
The installation is finally completed. Anaconda provides two ways to manage the created virtual machine: terminal and GUI. Environment:
Insert image description here

(4) Anaconda configures the Alibaba mirror source as the download source: Because Anaconda’s download source is overseas by default, if the domestic source is not configured, the download speed will be very slow, and many times there will be network errors and the download will fail;

Ali mirror source configuration official website: https://developer.aliyun.com/mirror/?serviceType=mirror&tag=&keyword=conda

  • Execute first conda config --set show_channel_urls yes Generate .condarc file under C:\Users\Administrator:

  • .condarcThat is the Anaconda mirror source configuration file. Replace the file content with the following:

    channels:
      - defaults
    show_channel_urls: true
    default_channels:
      - http://mirrors.aliyun.com/anaconda/pkgs/main
      - http://mirrors.aliyun.com/anaconda/pkgs/r
      - http://mirrors.aliyun.com/anaconda/pkgs/msys2
    custom_channels:
      conda-forge: http://mirrors.aliyun.com/anaconda/cloud
      msys2: http://mirrors.aliyun.com/anaconda/cloud
      bioconda: http://mirrors.aliyun.com/anaconda/cloud
      menpo: http://mirrors.aliyun.com/anaconda/cloud
      pytorch: http://mirrors.aliyun.com/anaconda/cloud
      simpleitk: http://mirrors.aliyun.com/anaconda/cloud
    
    
  • Next execute the following command:

    conda config --show-sources 或 conda config --show channels 		# 显示自己的Anaconda使用的镜像源
    
    conda clean -i 								# 清除索引缓存
    
    conda --version 或 conda -V						# 获取版本号
    
    conda update conda							# 更新当前conda
    
    
  • Configuration source related commands

    conda config --show-sources					# 显示自己的Anaconda使用的镜像源
    conda config --remove channels 源名称或链接 			# 移除某一镜像源
    
    conda config --remove-key channels 				# 移除其他,恢复conda默认镜像源
    
    
    
  • The Anaconda image source configuration file is a .condarc file, and the file location is generally located in this directory:C:\Users\自己用户名
    Insert image description here

(5) Why does Anaconda set up another channel?

In addition to the default Anaconda channel, setting up another channel can expand the types and versions of software packages available to users. Some users may need to use some non-standard or newer software packages in specific areas, and these packages may not be included in the default In the Anaconda channel, users can easily find and install these packages by setting up another channel;

  • Bioconda is a channel specially used to manage bioinformatics-related software. It already supports the installation of more than 2,700 bioinformatics-related packages;
  • Conda-forge is a channel maintained by the Conda community that contains many general-purpose software that are not included in the default channel;
  • Both channels can be added through settings to expand the types and versions of available software packages;
3. Anaconda provides two methods: terminal and GUI to manage the created virtual environment:
(1) Manage the virtual environment through Anaconda Prompt:

Insert image description here

(2) Manage the virtual environment through Anaconda Navigator:

Insert image description here

4. Configure environment variables: After configuring environment variables, we can replace the Anaconda Prompt terminal through the cmd terminal that comes with windoes, without having to open the Anaconda Prompt operation every time;

Before the environment variables are configured, the conda command is unavailable and the python environment is unavailable:
Insert image description here
Environment variables-> System variables-> Select Path-> Edit-> New- > Enter the anaconda3 path-> Finally click OK:

C:\ProgramData\anaconda3
C:\ProgramData\anaconda3\Scripts
C:\ProgramData\anaconda3\Library\bin
C:\ProgramData\anaconda3\Library\mingw-w64\bin

Insert image description here

Open the cmd terminal that comes with windoes and verify whether the configuration is successful:
Insert image description here

2. Anaconda conda common commands
1. Manage conda itself
(1) Check the conda version:
conda --version
(2) View conda’s environment configuration:
conda config --show
(3) Set the mirror source. Sometimes conda installation software will be very slow. Setting a domestic mirror can make the installation faster:

Please see the settings when the installation is successful;

(4) Query the help of a certain command:
conda create --help
2. Manage virtual environment

conda allows you to create independent environments that are isolated from each other. These environments are called virtual environments. Anaconda has a default environment named base, but it is not recommended to place programs in the base environment. Different virtual environments should be created and managed separately. different development projects;

(1) Check what virtual environments there are
conda env list			# 显示所有已经创建的环境
conda info -env
conda info --envs

(2) Create a virtual environment
conda create -n env_name(环境名称) python=x.x(python版本号)		# 创建环境
python --version(查看当前python版本号)

(3) Activate virtual environment
conda activate env_name(环境名称)
(4) Exit the virtual environment
conda deactivate
(5) Delete virtual environment
conda remove -n env_name(环境名称) --all			# 将指定的虚拟环境和该环境中所安装的包都删除

conda remove --name env_name  package_name		# 只删除虚拟环境中的某个或者某些包

(6) Export the environment so that you can restore the environment when needed, or provide it to others to create the exact same environment:

conda env export --name env_name > myenv.yml			# 获得环境中的所有配置

conda env create -f myenv.yml					# 重新还原环境

3. Conda package management related commands:
(1) Check the installation status of the package
conda list								# 查看当前环境中安装了哪些包

conda search package_name						# 查看当前Anaconda repository中是否有你想要安装的包

(2) Check whether a certain package has been installed, and support * wildcard fuzzy search
conda list pkgname  

conda list pkgname*  
  
(3) conda installation package, install a package in the current virtual environment
conda install package_name
conda install numpy=0.20.3				# 安装某个特定版本的包
conda install pkg_name -c conda_forge           	# 安装包的时候可以指定从哪个channel进行安装,比如该条命令表示从conda_forge安装某个包,而不是从缺省通道安装
conda install --name env_name package_name	 	# 在指定环境中安装包

(4) conda update package, update a package to its latest version
conda update numpy
(5) conda uninstall package
conda uninstall package_name						# 将依赖于这个包的所有其它包也同时卸载

conda remove package 							# 删除当前环境中的包

conda remove –-name env_name package 					# 删除指定环境中的包

(6) Clean anaconda cache

Like a miser, conda will save every historical installation package. The advantage is that it can be easily restored to the old historical version, but the disadvantage is that it takes up memory space;

conda clean --all	# 清除Conda缓存和临时文件,而不会删除已经安装的软件包和环境

4. Python version management

In addition to the python version that can be specified when creating a virtual environment above, the python version of the Anaconda base environment can also be changed as needed;

(1) Change the version to the specified version
conda install python=3.8

(2) Update the python version to the latest version
conda update python

python --version					# 更新完后可以用以下命令查看变更是否符合预期

5. It is not recommended to use pip to install tool packages in the conda environment. Mixing them can easily lead to confusion in library dependencies. Then suddenly the environment may collapse one day, and new packages cannot be installed, and conda updates cannot be performed.
6. Channel management
(1) Query which channels are included in the current configuration
conda config --get channels
(2) Add conda-forge channel
conda config --add channels conda-forge

(3) Remove conda-forge channel
conda config --remove channels conda-forge

3. Create a virtual environment:
(1) Create a virtual environment through Anaconda Navigator:

Environments -> Create -> Enter the name of the virtual environment, select the language, and select the version -> Create -> Wait for the environment to be created successfully;
Insert image description here
The virtual environment is created successfully. On the panel, you can view the name of the virtual environment and the packages installed in the virtual environment:
Insert image description here

(2) Create a virtual environment through Anaconda Prompt:

Open the cmd terminal -> Enter the conda command -> Wait for the environment to be created successfully;

$ conda create -n apple python=3.6 numpy # Create a virtual environment named apple, the python version is python3.6, and the numpy package is also installed

$ conda info --env # View environment
Insert image description here
You can also see the newly created apple virtual environment in the Anaconda Navigator panel:
Insert image description here

(3) Create the storage path of the virtual environment:

Insert image description here

4. Connect pycharm to the virtual environment just created:
1. Method 1: Connect to the python interpreter by configuring Conda Environment, recommended

File -> Settings -> Python Interpreter -> Add Interpreter -> Add Local Interpreter -> Conda Environment

Insert image description here

2. Method 2: Access the python interpreter by configuring VirtualenvEnvironment

File -> Settings -> Python Interpreter -> Add Interpreter -> Add Local Interpreter -> Virtualenv Environment

Insert image description here
operation result:
Insert image description here

5. Uninstall Anaconda
1. Find the Anaconda installation path and run C:\ProgramData\anaconda3\Uninstall-Anaconda3.exe to uninstall the program;
2. Delete the Anaconda installation folder, C:\ProgramData\anaconda3;
3. Delete the anaconda environment variable;
4. Delete related hidden files:
C:\Users\自己用户名\.anaconda

C:\Users\自己用户名\.conda

C:\Users\自己用户名\.continuum

5. Uninstalled successfully

おすすめ

転載: blog.csdn.net/qq_33867131/article/details/134197328