Worth collecting! Anaconda: a necessary manual for beginners!

Keywords: Python Anaconda IDE

introduction

Anaconda is a management tool for integrating packages and environments, dedicated to simplifying software management systems and deployments. In addition, we can also use conda instructions to easily create, activate, delete, exit and configure the virtual environment. pip is a python package management tool, which provides the functions of finding, downloading, installing and uninstalling Python packages.

In the process of daily development and use, the author will encounter some environmental management and related package operations from time to time. However, the network is full of a large number of old and ineffective solutions blogs, and sometimes even destroy the originally configured alchemy device, commonly known as "pit father". The following schemes have been effectively verified by the author and can be safely used.

Anaconda install, uninstall, copy

install

The Windows system can be downloaded and installed directly from the official website. Remember to add the path to the environment variable, or select "Add to path" during installation.

For Linux or OS system, go to Firefox browser to install the corresponding files, enter the specified directory, and execute the following instructions:

# The downloaded version of conda is different and can be downloaded on demand
bash Anaconda3-xxx.xx-Linux-x86_64.sh 

uninstall

Windows system:

Ctrl+S → Search control panel → Program uninstall → Right click Remove

Linux or OS System:

rm -rf ~/miniconda perhaps rm -rf ~/anaconda

copy

Under Linux system:

scp -r username@ip_address:/home/username/anaconda3/envs/$env_name ~/

The above instructions can copy the virtual environment under one server to the current location on another server, where:

Username: username, cvhub
ip_address: IP address, 172.31.20.xx
env_name: name of the virtual environment to be copied

The above variables can be replaced according to their actual environment.

Anaconda environment creation

conda create -n cvhub python=3.8 -y

The above instructions create a virtual environment named cvhub with Python version number of 3.8, - y means that the default configuration is followed, and there is no need to confirm later. Note: if it is not necessary, try not to select the latest Python version number during installation to ensure that subsequent configurations or installation packages will not be unsupported by the python version.

Anaconda environment activation

conda activate $pkg_name

$pkg_name is the name of the environment you created. Here is cvhub. (the same below)

Note: if. / bashrc is not configured properly under Linux system, conda instruction will be invalid. The following temporary instructions can be used to activate it:

source ~/.bashrc
source activate $pkg_name

Anaconda environment exit

conda deactivate

When exiting the environment, you do not need to add an extension. By default, you exit the current environment.

Anaconda environment delete

conda remove -n $pkg_name --all -y

Note that the – all parameter is mandatory here.

Anaconda environment configuration

Generally speaking, when we use the conda command to download the installation package, we will use the conda source download by default. However, due to the indescribable factors of xx, the download speed of the installation package will be extremely slow, and even the download interruption will often occur. I think this is no stranger to most novices. Are you one of them_

A better solution is to load domestic image sources, which can avoid this problem. Here are some commonly used image sources in China:

http://mirrors.aliyun.com/pypi/simple/ #Alibaba cloud
https://pypi.mirrors.ustc.edu.cn/simple/ #China University of science and technology
http://pypi.douban.com/simple/ #Douban
https://pypi.tuna.tsinghua.edu.cn/simple/ #Tsinghua University
http://pypi.mirrors.ustc.edu.cn/simple/ #University of science and technology of China

Of course, because of some xx factors, these sources are sometimes closed. As for whether they can be used, the key depends on luck. Here are some common instructions.

Add mirror source

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 --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --set show_channel_urls yes

The last line is used to display the downloaded channel source. Take pytorch download and download as an example, we should first go to the corresponding source website to see if there is a corresponding image link. For example, the pytorch website in Tsinghua source is this [1]. After entering, we can click to enter according to our operating system to check whether there is the version number we need. Of course, we should remove - c when installing with the official instructions of pytorch, and the default conda source will be used.

On the Linux system, you can try to modify the following configuration file, and then add the channels in it according to the image source address shown above:

sudo gedit ~/.condarc

Check mirror source

conda config --show # Displays the current configuration status
conda config --get channels # View current configuration status

After finalizing the first line of instruction, some columns of information will be printed. We need to find the information under the keyword "channels" to view it.

Delete mirror source

# Delete the current mirror source
conda config --remove channels  https://pypi.mirrors.ustc.edu.cn/simple/ 
# Remove all mirror sources
conda config --remove-key channels

Anaconda common Instruction Summary

conda -h # By using the help command, you can see other command functions at a glance
conda --version # View current conda version
conda list # View currently installed packages
conda install $pkg_name # Installation package
conda uninstall $pkg_name # Uninstall package
conda info --envs # Displays the virtual environment currently being created
conda update conda # Check and update to the latest version of conda
conda update --all # Update all installation packages
conda update python # Update python to the latest version
conda remove --name $pkg_name # Delete a package in the current virtual environment
conda remove --name $env_name  $pkg_name # Delete a package in a virtual environment
conda env export -n $env_name -f $env_name.yml # Export configuration as yml file
conda env upload -f $env_name.yml # Upload yml file
conda create -n $env_name -clone $cp_env_name  # Copy the current virtual environment
conda search $pkg_name # Search for information about a package
conda clean -p # Clean up unused packages
conda list -e > requirements.txt # Export all packages in the current virtual environment

Complete collection of pip instructions

  • Update to the latest version
pip install --upgrade pip
  • Set mirror source
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/
  • pip delete mirror source
pip config unset global.index-url
  • pip view current mirror source
pip config list
  • pip view current mirror source
pip config list
  • pip installation package
pip install $pkg_name -i http://Pypi.doublan.com/simple # temporarily uses the specified image source installation package, which can be changed by yourself

If an untrusted error is reported, the following instructions can be added to solve it:

pip install -U scikit-learn -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

If you want to set it at one time, you can modify ~ /. pip/pip.conf file under Linux. If it does not exist, create it manually:

# vim ~/.pip/pip.conf
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = https://pypi.tuna.tsinghua.edu.cn

Under Windows system, create a PIP folder under the current system user directory, such as C:\Users\Administrator\pip, and then create and edit the pip.ini file in the current directory:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = https://pypi.tuna.tsinghua.edu.cn

The image source here can be modified as needed. In addition, we can also set it under pychar:

File → Settings → Project:Name → Project Interpreter → add module '+' → Manage Repositories → add the corresponding image source.

  • Generate the requirements file in the current environment with pip
pip freeze > requirements.txt
  • Install the requirements file in the current environment with pip
pip install -r requirements.txt

Summary of frequently asked questions

  • If you cannot find the relevant library with pip prompt, you can switch to the next step and install it with conda command;

  • If the download speed of a source is full, you can switch to different image sources. For example, sometimes the Tsinghua source is only 10k and the Douban source is more than 2M;

  • If you are prompted that the connection times out and there is a problem with the proxy settings, you can check whether your vpn is turned on globally and change it to PAC mode;

  • If "conda Collecting package metadata (current_repodata.json): failed" appears, it indicates that the currently set image source may be invalid. You can directly execute the following instructions in turn, and then reinstall the package you need:

cls
conda config --remove-key channels
conda update conda
conda update --all
conda config --add channels conda-forge
conda config --set channel_priority flexible

summary

CVHub team is keen to output high-quality original dry goods articles. Each article has been carefully crafted by us and is committed to cooking more delicious CV technology delicacies for readers. If this article can help you, please don't hesitate your interaction, just click three times. Your praise and forwarding is our greatest encouragement. We will write more wonderful and high-quality articles to give back to readers in the future.

References

[1]https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/: t

Technical exchange

Welcome to reprint, collect, gain, praise and support!

At present, a technical exchange group has been opened, with more than 2000 group friends. The best way to add notes is: source + Interest direction, which is convenient to find like-minded friends

  • Method ① send the following pictures to wechat, long press identification, and the background replies: add group;
  • Mode ②. Add micro signal: dkl88191, remarks: from CSDN
  • WeChat search official account: Python learning and data mining, background reply: add group

Posted by duklaprague on Sat, 02 Oct 2021 18:42:12 -0700