I used to learn Python and machine learning in the separate way of Python and PIP. Now I want to switch from TensorFlow to Pytorch deep learning framework. However, due to the confusion of PIP management, I find that Pytorch framework fails to be called, so I am determined to make the whole management system more orderly. So I went to Anaconda, and I got the following article. As a first note, many people say that installing Anaconda requires uninstalling Python to avoid conflicts. However, after I configured Anaconda, I found that the original Python did not affect the Python versions or packages in my Anaconda environment. However, the old Version of Python and the new version of Anaconda would be redundant, and one day you might introduce a programming environment that doesn’t work. The bottom line is: Anaconda does not mean you have to clean up the separate Python, but it is recommended that you leave Anaconda alone. Just climbed out of the Python3.7+ PIP to Anaconda+ Conda configuration pit. Write a blog to record some details. Here we go.

1. Why install Anaconda?

The following points are I think of which to write which, relatively scattered, we see a general, understand a meaning on 🆗

  • Anaconda is a Python distribution. It is an integrated environment manager for Python. It contains Python and many libraries, including Numpy, Pandas, and other libraries. It also includes a package manager called Conda.
  • Once you have Anaconda installed, you have installed the Python compiler and many other commonly used packages (other than third-party frameworks and the like).
  • Anaconda can be operated under its GUI, the Anaconda Navigator. This visual package management is much more convenient than installing Python alone and PIP alone at the end! Moreover, the terminal PIP installation items were relatively difficult to manage the installation paths (so I had left drive C carrying too many “sole” s), and Anaconda was an integrated manager, with each package, and the location of each virtual environment, clearly present
  • My biggest feeling after using Anaconda is that I can create multiple virtual environments! If you want to use both TensorFlow and Pytoch, and you don’t want different libraries to collide, then Anaconda’s virtual environment is the perfect choice! I put different frameworks, different libraries in different virtual environments, independent of each other, just need to switch to the environment I need to program every time! Beautiful, beautiful, beautiful

2 Download and install Anaconda

Some people here say that it is not recommended to download in the official website, relatively slow. But I feel that the download is quite fast, this will not describe in detail, attached to the official website link:Anaconda InstallersThe version I installed is shown below:As for the installation process, most of the steps are casual, but! Two steps are critical! Just put the picture in. In the picture below, I personally feel that there is no problem, because I chose Just Me, and it can be used normally.Attention below!! The first one is not up, the second one is up!! Don’t ask why, summarize other people’s stomping pit points…

3 Configure system environment variables for Anaconda

If you’ve ever written code, then environment variables are familiar, like C, Go, Java… Similarly, to configure environment variables in advanced system Settings, follow the tutorial to add the following items to the path of the system variable:

E:\Anaconda\Library\mingw-w64\bin E:\Anaconda\Library\mingw-w64\bin E:\Anaconda\Library\usr\bin E:\Anaconda\Library\bin (jupyter Notebook)Copy the code

Attention! Add your own Anaconda path, not the E in the example above:

At this point, your Anaconda has been configured. Open CMD again to check if the configuration is successful. See the following figure.

4 Create your own virtual environment in Anaconda

Find the Anaconda Navigator management software in the system, this is the interface. It’s clear, right? Let me introduce the functions: the interface as shown in the figure is the function panel under the default environment base (root), where Prompt is the command line terminal for managing the environment, Jupyter Notebook, Spyder, PyCharm and so on are all software for writing programs (choose according to your own situation).Next comes the big deal — Environment. Look at the following figure. In the default base environment, you already have many packages and libraries on the right, which can be directly imported. However, the default environment is frameless, such as TensorFlow, Pytorch, etc. The reason I changed Anaconda this time was to make it easier to use Pytorch, so the following tutorial follows the Pytorch framework in the virtual environment I created.First, click Create on the left to Create a new virtual environment, then name and select the Python version. I chose Python3.8.

After creating the virtual environment, you will notice that there is an environment option in the previous interface. If you select Pytorch_envs, any software and tools opened in the interface will be based on the Python version of the environment, as well as various packages and libraries.

5 Use the Conda installation package and framework in the virtual environment

If you are in a GUI interface, you can obviously install it by searching, as shown in the figure below.Or, in the most general way, install by command. This section describes two ways to open it. The first way is to click â–² on the right of the environment and select Open terminal.The second option is to click Prompt in the specified environment.If the environment is not specified, run the activate < environment name > command to activate the environment.First, it should be clear that installing something like Pytorch or Matplotlib using the default image source will be very slow or even fail. To prevent this problem,It is necessary to change the mirror source first. More mainstream mirror source: Tsinghua source, douban source, source of the University of Science and Technology… Douban source I am not familiar with, qingqingyuan â•¥ : â•¥ a long story, have banned before, now may not keep up with the maintenance, so the installation of things waiting for 30min popup HTTPError error, meaning is unable to get from this mirror source needed things.

This time is about to decisively change mirror source – the university of Science and Technology mirror source. Just two lines of command:

conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
Copy the code

But if you still retain tsinghua mirror source, please delete completely, otherwise when downloading things are likely to pull to Tsinghua mirror source again. The command to delete the mirror source is also simple:

conda config --add channels <URL>
Copy the code

How do I view this URL? The following command line:

conda config --show channels
Copy the code

Remove all mirror sources other than USTC. (The image above is the result I kept after deleting, just one line of mirror source.)

  • Conda installation pyTorch download too slow fixed
  • Conda Domestic Image modification (latest version)

The command is simple and very similar to PIP:

Conda install < package/library name >Copy the code

If you are installing the Pytorch framework, you will need to go to the official website to see what the installation command is.Pytorch InstallationThe following is my selection, and I can remove -c when typing the command

Conda install pyTorch Torchvision torchaudio cudatoolkit= 10.2-c pytorch conda install Pytorch Torchvision torchaudio Cudatoolkit pytorch = 10.2Copy the code

Use the Conda List to view all the packages, libraries, and frameworks in the virtual environment.

All of these operations (creating a virtual environment, switching environments, installing libraries) can be done using the Prompt command line alone. For details, see the link:

  • Conda Pip manages the environment and installation packages and replacement sources
  • Anaconda detailed installation and use tutorial

Some simple examples:

  • To create a virtual environment: conda create -n env_name python=3.8
  • To delete a virtual environment, run conda remove -n env_name –all
  • Activate the virtual environment: activate env_name

With the help of Anaconda, I have started to write deep learning, run integration learning and do yoloV5 target detection in my own virtual environment these days. At least with Anaconda, I will no longer be confused in the environment where I run the program. Just to mention some of the recent comparisons 👇 if we don’t have a particular need to open Anaconda Navigator, we try not to open the command line or IDE from it, because it takes a bit of time to open it, and I’ve either finished typing or already had Pycharm on. How do you do that? The easiest way to do this is to put the Anaconda Prompt shortcut on your desktop and just click and use it.After all, if we think about it, this is actually the same thing as CMD! Use CMD/Powershell (as it turns out, powershell does not display activate), and use CD/activate to activate the virtual environment. Common commands are as follows:

Activate < Virtual environment name > conda List conda install < package name > conda install matplotlibCopy the code

Is short, the core of the execution or in terminal, to fast, with good terminal – including, after more than write a larger program, in terminal commands to compile executable program RUN the program more convenient than using an IDE, but also saves the IDE environment configuration of the software, even the compiler only when a text editor (is a digression, can? The feeling of writing too much code)

Programming with an IDE

6.1 PyCharm

PyCharm Configuration is straightforward. Turn on PyCharm and select the build environment in the Configuration to Anaconda’s specified virtual environment.Test environment code (matplotlib and Pytorch installed by built-in Numpy)

# -*- coding: utf-8 -*-
"""
Created on Mon May 24 20:11:56 2021
@author: Zeng Wenxuan
"""
import torch
import numpy as np
import matplotlib.pyplot as plt

a = torch.rand((2.3))
print(a)
print("Hello Anaconda!")

x = np.linspace(-0.5.0.5.50)
y = x ** 3
plt.plot(x, y, 'r')
plt.scatter(x, y)
plt.show()
Copy the code

6.2 the Spyder

If you haven’t installed Spyder before, I recommend you install it under the Anaconda interface. And select the virtual environment you specify! Otherwise, it is the default base environment. Here is the Spyder software with Pytorch_envs by default.The py file can be saved in any directory you want, so there is no need to worry about the file path. Besides, this is the first time I use Spyder to write code. I did not expect this interface to let me like, and Matlab similarity is high, code block, terminal, image, variable list is separate, very clear ~

6.3 Jupyter Notebook

Jupyter Notebook is a Web programming tool I have been using to learn Python and machine learning. The main use scenario is programming learning and practice. It can present the operation results of each module, which is very suitable for beginners debugging, but not suitable for writing large projects. The first way to open is to click Jupyter under the specified environment of Anaconda.But I encountered a problem ah, the root directory of the launch of Jupyter is in the C disk User, I have not thought of a way to enter the D disk from the directory… â•¥ man â•¥… Which good Samaritan know can give me a message ~ thanks in advance! (2021.5.26 noon: some friends told me that the problem is the default path of Jupyter, you can modify the default configuration: refer to this linkJupyter Notebook file default storage path and change methodAnother common way to do this is to go into the Jupyter Notebook at the command line, which is what I started in Windows under CMD. The method is simple:cd <abs_address>+jupyter notebook

7 References

I’ve learned a lot from many of these blogs, but each one has its flaws. For example, some of them are very detailed about the installation process, some of them are very detailed about the package installation process, and some of them focus on the flaws in one area. The purpose of writing this summary is to complete the whole installation process, configuration process and optimization process, and record all kinds of problems encountered by myself and everyone for reference in the future study. Here are some of my most helpful links:

  • How to install Anaconda3

The installation process and environment configuration is very concise and clear

  • Anaconda Detailed Installation and Use tutorial (with graphics)

This article lacks some description of environment configuration, but the second half has a good understanding of Anaconda’s virtual environment and various tools

  • Anaconda Installation Tutorial (Using Spyder)

This is the first time that I came into contact with Spyder programming

  • Conda installation pyTorch download too slow fixed

Install the Pytorch conda with -c. Install the Pytorch conda with -c. Install the Pytorch conda with -c

  • Conda Pip manages the environment and installation packages and replacement sources

Anaconda installation tutorial ↑ These two chapters have detailed environment, package management commands