Home > Article > Backend Development > Anaconda's Beginner's Guide
This time I will bring you a complete guide to how to use Anaconda for novices. What are the precautions for novices to use Anaconda? The following is a practical case, let’s take a look.
Preface
Python is easy to use, but it is not easy to use it well. The more troublesome ones are package management and different versions of Python, especially when you use Windows. In order to solve these problems, there are many distributions of Python, such as WinPython, Anaconda, etc. These distributions package python with many commonly used packages to facilitate direct use by pythoners. In addition, there are tools such as virtualenv and pyenv to manage virtual environments. (Recommended learning tutorial: Python Video Tutorial)
I personally tried many similar distributions and finally chose Anaconda because of its powerful and convenient package management and environment management functions. This article mainly introduces Anaconda, understanding of Anaconda, and briefly summarizes related operations.
Anaconda is a Python distribution used for scientific computing. It supports Linux, Mac, and Windows systems. It provides package management and environment management functions, and can easily solve many problems. Problems with coexistence and switching of python versions and installation of various third-party packages. Anaconda uses the tool/command conda to manage packages and environment, and already includes Python and related supporting tools.
Here we first explain the differences between the concepts of conda and anaconda. conda can be understood as a tool and an executable command. Its core function is package management and environment management. Package management is similar to the use of pip, and environment management allows users to easily install different versions of Python and switch quickly. Anaconda is a packaged collection, which is pre-installed with conda, a certain version of python, many packages, scientific computing tools, etc., so it is also called a distribution of Python. In fact, there is also Miniconda. As the name suggests, it only contains the most basic content-python and conda, as well as related necessary dependencies. For users with strict space requirements, Miniconda is an option.
Before going below, let me explain the design concept of conda - conda treats almost all tools and third-party packages as packages, even python and conda itself! Therefore, conda breaks the constraints of package management and environment management, and can easily install various versions of python and various packages and switch easily.
For the download page of Anaconda, see the official website for download. Linux, Mac, and Windows are all supported.
When installing, you will find that there are two different versions of Anaconda, corresponding to Python 2.7 and Python 3.5. The two versions are actually the same except for this difference. We will see later that it is not essential which version to install, because through environment management, we can easily switch the runtime Python version. (Since the Pythons I commonly use are 2.7 and 3.4, I tend to install Anaconda directly for Python 2.7)
After downloading, just follow the instructions to install it. Here's a reminder: Try to install according to Anaconda's default behavior - do not use root permissions, only install for individuals, and set the installation directory to your personal home directory (it doesn't matter for Windows). The advantage of this is that different users on the same machine can install and configure their own Anaconda without affecting each other.
For Mac and Linux systems, after Anaconda is installed, there is actually just an additional folder (~/anaconda) in the home directory, and Windows will write it to the registry. During installation, the installation program will add the bin directory to PATH (Linux/Mac writes ~/.bashrc, Windows adds it to the system variable PATH). These operations can also be completed by yourself. Taking Linux/Mac as an example, the operation to set PATH after installation is
# Add anaconda's bin directory to PATH. Depending on the version, it may also be ~/anaconda3/bin
echo ' export PATH="~/anaconda2/bin:$PATH"' >> ~/.bashrc
# Update bashrc to take effect immediately
source ~/.bashrc
Configured After PATH, you can check whether it is correct through the which conda
or conda --version
command. If the version corresponding to Python 2.7 is installed, run python --version
or python -V to get Python 2.7.12::Anaconda 4.1.1 (64-bit), which also shows that the default version of this distribution The environment is Python 2.7.
Conda’s environment management function allows us to install several different versions of Python at the same time and switch freely. For the above installation process, assuming that we are using the installation package corresponding to Python 2.7, then Python 2.7 is the default environment (the default name is root, please note that root does not mean super administrator).
Suppose we need to install Python 3.4. At this time, the operations we need to do are as follows:
# Create an environment named python34, specify the Python version is 3.4 (it doesn’t matter if it is 3.4.x, conda will automatically find the latest version in 3.4.x for us)
conda create --name python34 python=3.4# At this point, enter
python --version
# again to get `Python 3.4.5::Anaconda 4.1.1 (64-bit)`, that is, the system has Switched to the 3.4 environment# If you want to return to the default python 2.7 environment, run
deactivate python34 # for Windows
source deactivate python34 # for Linux & Mac# Delete one Existing environment
conda remove --name python34 --all# After installation, use activate to activate an environment
activate python34 # for Windows
source activate python34 # for Linux & Mac
# After activation, you will find that the word python34 is added to the terminal input area. In fact, what the system does at this time is to remove the default 2.7 environment from the PATH, and then add the commands corresponding to 3.4 to the PATH
Different python environments installed by users will be placed in the directory ~/anaconda/envs
. You can run conda info -e
in the command to view the installed environment. , the currently activated environment will be displayed with an asterisk or brackets.
Note: Some users may often use the python 3.4 environment, so directly add the bin or Scripts below ~/anaconda/envs/python34
to PATH and remove the bin directory corresponding to anaconda. This method, how can I put it, is possible, but I always feel that it is not so elegant...
If you directly change PATH as mentioned above, you will find that the conda command cannot be found again (of course it cannot be found) , because conda is in ~/anaconda/bin), what should we do at this time? There are two methods: 1. Explicitly give the absolute address of conda 2. Also install the conda tool in the python34 environment (recommended).
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:
# Install scipy
conda install scipy
# conda will search scipy-related information and dependent projects from a remote location. Python 3.4, conda will install numpy and mkl (computing acceleration library) at the same time# View the installed packages
conda list
# The latest version of conda is searched from the site-packages folder Installed packages do not depend on pip, so packages installed through various methods can be displayed.
Some common operations of conda are as follows:
# View the current environment Download installed packages
conda list# View installed packages in a specified environment
conda list -n python34# Find package information
conda search numpy# Install package
conda install -n python34 numpy
# If you do not specify the environment name with -n, it will be installed in the current active environment
# You can also specify a certain environment with -c channel installation# Update package
conda update -n python34 numpy# Delete package
conda remove -n python34 numpy
Already mentioned before Yes, conda treats conda, python, etc. as packages. Therefore, you can use conda to manage the versions of conda and python, such as
# Update conda and keep conda up to date
conda update conda# Update anaconda
conda update anaconda# Update python
conda update python
# Assuming the current environment is python 3.4, conda will upgrade python to 3.4.x The latest version of the series
Supplement: If you create a new python environment, such as 3.4, run conda create -n python34 python=3.4
, conda will only install python 3.4 related ones Required items, such as python, pip, etc. If you want the environment to be like the default environment and install the anaconda collection package, you only need:
# Install the anaconda package collection in the current environment
conda install anaconda# Combined with the command to create an environment, the above operations can be combined into
conda create -n python34 python=3.4 anaconda
# You can also not install them all, just install the packages you need according to your needs
If you need to install many packages, you will find that the conda download speed is often very slow because the Anaconda.org server is abroad. Fortunately, the Tsinghua TUNA image source has the image of the Anaconda warehouse. We can add it to the conda configuration:
# Add Anaconda's TUNA mirror
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
# Mirror in TUNA's help The address has quotation marks and needs to be removed# Set the channel address to be displayed during search
conda config --set show_channel_urls yes
After executing the above command, ~/. The condarc (Linux/Mac) or C:\Users\USER_NAME\.condarc file records our configuration of conda. Creating and editing the file directly has the same effect.
Anaconda has the characteristics of cross-platform, package management, and environment management, so it is very suitable for quickly deploying Python environments on new machines. In summary, the entire installation and configuration process is as follows:
Download Anaconda, install
Configure PATH (bashrc or environment variable), change TUNA Mirror source
Create the required python environment of different versions
Just Try!
cheat-sheet Download:
Conda cheat sheet
I believe you have mastered the method after reading the case in this article, please come for more exciting information Pay attention to other related articles on php Chinese website!
Recommended reading:
Operation excel to read and write data
##Detailed steps for using unittest test interface in python
The above is the detailed content of Anaconda's Beginner's Guide. For more information, please follow other related articles on the PHP Chinese website!