Anaconda installation and VSCode configuration Python related environment

1. Introduction and installation of Anaconda

Anaconda refers to an open source Python distribution, which contains more than 180 scientific packages such as conda and Python and their dependencies. Because it contains a large number of scientific packages, the download file of Anaconda is relatively large (approximately 531 MB). If you only need certain packages, or need to save bandwidth or storage space, you can also use Miniconda, a smaller release version (only conda and Python).

1.1 Basic introduction

Anaconda includes Conda, Python, and a lot of installed toolkits, such as numpy, pandas, etc.
Miniconda includes Conda and Python.
Conda is an open source package and environment manager that can be used to install different versions of software packages and their dependencies on the same machine, and can switch between different environments.

1.1.1 conda package management

  • conda clean
  • conda config
  • conda create
  • conda help
  • conda info
  • conda install
  • conda list # Luo lists all installed scientific packages and their dependencies
  • conda package
  • conda remove
  • conda search
  • conda uninstall
  • conda update
  • conda upgrade

After installing Anaconda, we can easily manage the installation package (installation, uninstallation, update).

A. Installation package

The package management function of conda is the same as pip. Of course, you can choose pip to install the package.

 #安装 matplotlib   
 conda install matplotlib

or

pip install matplotlib

B. Uninstall the package

# 删除包  
conda remove matplotlib  

or

pip uninstall matplotlib

C. Update package

 # 包更新  
conda update matplotlib  

D. Query the installed packages

 # 查看已安装的所有包  
conda list   

or

pip3 list

or

# 查看指定pandas包的版本信息
conda list pandas

E. Environmental management

conda can create different operating environments for your different projects.

1. Create a python2.7 environment

#创建python2.7版本的环境
conda create -n python27 python=2.7

In the above command, python27 is the name of the setting environment (-n means that python27 after the command is the name of the environment you want to create)
Note: When creating an environment, you can specify the Python version to be installed in the environment. This is useful when you are using code in Python 2.x and Python 3.x at the same time.

2. Enter the environment

#进入我刚创建的python27环境
conda activate python27

If we forget the name, we can use it first

#查看所有环境
conda env list

After entering, you can see the environment name (python27) in the terminal prompt. Of course, when you enter the environment, you can also use conda list to view the default installation package in the environment.

3. Leave the environment

#离开当前环境
deactivate

4. Shared environment

The shared environment is very useful. It allows others to install all the packages used in your code and ensure that the versions of these packages are correct. For example, if you develop a system, you have to submit it to the people of the project deployment system to deploy your project, but they don't know which python version you were developing at the time, and which packages and package versions were used. What to do about this? You can use in the terminal of your current environment:

#将你当前的环境保存到文件中包保存为YAML文件
conda env export > environment.yaml  

Save your current environment to a file and save the package as a YAML file (including the Pyhton version and the names of all packages). The first part of the command conda env export is used to output the names of all packages in the environment (including the Python version). You can see the exported environment file path in the terminal. When sharing code on GitHub, it is best to also create environment files and include them in the code base. This makes it easier for others to install all the dependencies of your code.

How to use the exported environment file in other computer environments?
First enter your environment in conda, such as

conda activate python27

Then use the following command to update your environment:

#其中-f表示你要导出文件在本地的路径,所以/path/to/environment.yml要换成你本地的实际路径  
conda env update -f=/path/to/environment.yml  

Can also

conda env create -f environment.yaml // 用配置文件创建新的虚拟环境

For users who do not use conda, We usually use the following command to export a txt file and include it:

pip freeze > environment.txt   

Then I included the file in the code base of the project, and other project members can use this file to install the same development environment as mine even if conda is not installed on his computer:
he enters the python command environment on his computer, Then run the following command to install the packages required by the project:

#其中C:\Users\Microstrong\enviroment.txt是该文件在你电脑上的实际路径。  
pip install -r C:\Users\Microstrong\enviroment.txt  

5. List the environment

Sometimes I forget the name of the environment I created, then use

conda env list 

You can list all the environments you created.
You will see a list of environments, and there will be an asterisk next to your current environment. The default environment (that is, the environment used when you are not in the selected environment) is named base.

6. Delete the environment

If you no longer use an environment, you can use the following command.

#删除指定的环境(在这里环境名为 python27)。  
conda env remove -n python27  
conda remove -n  python27 --all // 删除 python27环境及下属所有包

1.1.2 Anaconda3 default installation package list

See Baidu Encyclopedia- Portal

1.2 Download URL

Official website download address : https://www.anaconda.com/products/individual
Tsinghua University open source software mirror download address : https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/?C=M&O=A and
above Both addresses can be downloaded, and the domestic site is faster.

1.3 Installation of Aanconda3

Figure oneInsert picture description hereInsert picture description hereInsert picture description hereInsert picture description here
After the installation is over, remove the two check boxes on the interface in the last step, and click the finish button directly.
Test whether the installation is successful
1. Open cmd input conda --versionnormal output version number represents conda successful installation
Insert picture description here
to avoid possible errors, we enter conda upgrade --all first of all at the command line toolkit upgrade

2. Enter the python output version number in the command line interface and enter python to represent the successful installation of python
Insert picture description here

3. Set up the Anaconda mirror to speed up the download of the package
. It is very convenient to install the required Python by using conda install package name, but the official server is abroad, so the download speed is very slow. Tsinghua University in China provides Anaconda warehouse mirroring, we only need to configure Anaconda Configure file, add Tsinghua's mirror source, and then set it as the first search channel, then execute the following commands under the cmd command line:
(refer to https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/ )

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

If you need to delete the mirror source, use the following command

conda config --remove channels 'https://repo.continuum.io/pkgs/main/'

After the configuration, you can test it. The installation of third-party packages is obviously fast

1.4 Check environment variables

If it is windows, you need to go to Control Panel\System and Security\System\Advanced System Settings\Environment Variables\User Variables\PATH to
check whether the following path has been added to the variable value of Path:
D:\python\anaconda3;
D: \python\anaconda3\Library\mingw-w64\bin;
D:\python\anaconda3\Library\usr\bin;
D:\python\anaconda3\Library\bin;
D:\python\anaconda3\Scripts;

2. VSCode install python

2.1 Install Python plugin

Open vscode and click the extension button on the left menu bar (or shortcut key ctrl+shift+x) to enter Python, follow the prompts in the figure below to find the python plugin and install it
Insert picture description hereAfter the installation in the previous step, configure the python interpreter path to the python.pythonPath parameter. The specific steps are as shown in the figure below:
Insert picture description hereAfter the above python interpreter path is configured, you can compile and run python.

2.2 Install flake8 static code checker

First enter cmd and use the command

conda list flake8

Check if flake8 is installed. If you install it, you can check the version number:
Insert picture description here
if the full version of Anaconda is installed on your computer, flake8 is installed by default. If you install the miniconda version, you need to install flake8 using the following command

conda install flake8

Or install using pip

pip3 install flake8

After the installation is successful, follow the steps below to enable the flake8 code check:
Insert picture description herethen configure the parameters of flake8, configure the flake8 parameters in python.linting.flake8Args, here change the default line of 79 characters for flake8 to 248

"python.linting.flake8Args": [
        "--max-line-length=248"
    ]

Insert picture description hereMethod: search for linting in the settings, first enable flake8, then set flake8 Args, the set parameter is "–max-line-length=248" Note the setting with double quotes

2.3 Install code formatting tool yapf

Open cmd and enter the command to check whether yapf is installed

conda list yapf

Need to install if not installed

The vscode configuration is as follows:
Insert picture description here
Method: search python.formatting.provider in the settings, then select yapf

2.4 Configure smart prompts

First open the configuration, search for autoComplete in the user configuration, open add Brackets, the effect is as follows:
Insert picture description here
Specific opening steps:
Insert picture description hereNext, configure the installed third-party package directory in python.autoComplete.extraPaths, the effect is as follows:
Insert picture description here

Note that you need to configure according to your own anaconda3 or python installation directory. The anaconda3 I used is installed in the D:\python\anaconda3 directory. If you install the python environment, configure it in the python directory.

The specific configuration steps are shown in the figure below:
Insert picture description here
Then use the command to check whether the jedi package is installed, this package is an automatic completion tool

conda list jedi

Insert picture description here

Then set Python.language.Server to jedi. The
specific steps are as follows
Insert picture description here

Guess you like

Origin blog.csdn.net/u011930054/article/details/112383654