[conda] Use conda for environment management and package management

Using conda for environment management and package management

Using conda for environment management and package management

conda is the environment manager and package manager in anaconda.

All conda operations occur within the command line, and we can open Anaconda Prompt for operations.

1. Check conda

Before using conda, we first check whether conda has been installed and whether the current version is the latest.

# 检查conda是否已经安装好,此命令会返回你安装Anaconda软件的版本
conda --version
>> conda 4.3.40

# 通过以下命令升级conda到最新版本
# 如果有新版本可用,在提示proceed ([y]/n)? 中输入y进行升级
conda update conda

2. Environmental management

Environment management is a good habit in using Python. If you don't want to have to reinstall Python and the system over and over again, then environment management is a very necessary part of the process of learning Python. Now we use conda for environment management.

  • Create environment
# 创建一个环境名为py34,指定Python版本是3.4
#(不用管是3.4.x,conda会为我们自动寻找3.4.x中的最新版本)
conda create --name py34 python=3.4
# 通过创建环境,我们可以使用不同版本的Python
conda create --name py27 python=2.7
  • activate environment
# 在windows环境下使用activate激活
activate py34

# 在Linux & Mac中使用source activate激活
source activate py34

After activation, you will find that there are more words (py34) in the terminal input area, which means that we have entered the py34 environment.

  • Exit environment
# 在windows环境下使用deactivate
deactivate

# 在Linux & Mac中使用source deactivate
source deactivate
  • Delete environment

If you don't want this environment named py34, you can delete this environment with the following command.

conda remove -n py34 --all

You can view the existing environment list through the following command. Now py34 is no longer in this list, so we know that it has been deleted.

conda info -e

3. Package management

We use conda to install, uninstall and update third-party packages.

For package downloading, we can first set up a domestic mirror. This is because the server of http://Anaconda.org is abroad, so conda is often very slow when downloading packages. Fortunately, the Tsinghua TUNA image has the image of the Anaconda warehouse. We can solve this problem by adding it to the conda configuration.

# 添加Anaconda的TUNA镜像
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes

Next, we install the package. Please enter the specified environment (such as py34 in the previous section). Here we take pandas (a data processing and analysis package) as an example.

  • View installed packages
#使用这条命令来查看在当前环境中,已安装的包和对应版本
conda list
  • Find installable packages
#我们可以通过search命令检查pandas这个包是否可以通过conda来安装
#命令返回了这个包的信息,所以是可以通过conda安装的
conda search pandas
  • Installation package
#通过install安装pandas
#如果pandas已经存在于环境中,会提示已经安装,否则在提示proceed ([y]/n)? 中输入y进行安装
conda install pandas
  • Update package
#通过update更新pandas
conda update pandas
  • Uninstall package
#通过remove卸载pandas
conda remove pandas

The above is the installation, update and uninstallation of packages by conda. It is worth mentioning that conda treats conda, python, etc. as packages. Therefore, you can use conda to manage the versions of conda and python, for example

# 更新conda到最新版本,这里conda被当作一个包处理 
conda update conda 

# 同样的,也可以更新anaconda到最新版本
conda update anaconda

# 更新python
# 例如我们所启用的环境是py34,使用的是python3.4,那么conda会将python升级为3.4.x系列中的最新版本
conda update python 

Guess you like

Origin blog.csdn.net/weixin_42473228/article/details/131091062
Recommended