The relationship between Anaconda&Python&Pycharm and python package installation and environment construction problems

After the headache of installing software, I finally understood the relationship between Anaconda, python, Pycharm and the installation of various python packages

一、 Anaconda,python,Pycharm

1. If you want to use python for deep learning, you first need a development environment. To put it bluntly, it is a software for programming python programs. It is generally more convenient to choose pycharm.
2. Then you need to load a python compiler in pycharm. At this time, you can install a python. There will be a lot of [python installation tutorials] on the Internet.
3. You can also install Anaconda and then the compiler in pycharm select [python.exe] in the conda environment as the compiler. And after installing Anaconda, you don't need to install python, it has already installed python automatically.

2. Select the python compiler in Anaconda for Pycharm

As mentioned earlier, the second method is recommended, so the following is to install various python packages under the second condition.

1. After installing pycharm and Annaconda (very simple, there are many tutorials on the Internet), start specifying the compiler for pycharm. The specific steps are as follows:

①Click [file] in the upper right corner of pycharm ②Click
insert image description here
[settings], select [project]—>[Python Interpreter]
insert image description here

③Click the small button in the upper right corner
insert image description here
④Click【Add】
insert image description here

⑤Click【conda environment】
insert image description here

⑥Then click [existing environment]
insert image description here
⑦Click […] (as shown in the figure below) to find the python.exe file under the installation Anaconda folder and select it
insert image description here
insert image description here

In this way, you have selected a compiler for pycharm. Generally, the python compiler in the default base environment in Anaconda is selected. Of course, you can build environments with different python versions and package versions according to different codes, which is also the convenience of Anaconda. In this way, it will be much more convenient to pack or run code in the future. The packaging process is as follows:

3. Install, uninstall and update various python packages and libraries

There are two ways, I usually use the second way

1. Method 1: Use pip to install in the command window (cmd)

Note: In this case, make sure that the path of Anaconda has been added to the environment variable of the system, otherwise the installed command may not be recognized

1.1 Installation

①Click win+r and enter cmd to bring up the command window:
insert image description here
insert image description here
②Then enter the installation command in the window. I usually use Douban source, so the installation is quicker.
Note below: If you create a different environment, you need to install it first. enter:

activate 环境名

Only in this way can you ensure that you have installed it in the corresponding environment. Many people will prompt that there is no such package after installing the corresponding package. It is very likely that you have not installed it in the environment used by the current project file, which is why it is recommended The reason for using Anaconda is that it can create an environment with a specified python version, and activate the corresponding environment when installing the python package to install and call normally. For Anaconda to create different environments, see the fourth article below.
Then enter the following command to install the seaborn package, install other packages and change the package name

pip install seaborn -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
1.2 Uninstall

The steps are the same as the installation, except that the command in the second step is directly written as:

# pip uninstall 要卸载的包名
# 下面以卸载seaborn包为例
pip uninstall seaborn

Just

1.3 Update

Step 1 is the same as the installation
. Step 2 is roughly the same, except that the installation command is changed to:

# 下面是以更新pytorch、torchvision为例,更新别的包把这两个包的名字替换一下即可
pip install --upgrade pytorch torchvision -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

Just

2. Method 2: Anaconda installs the python package

2.1 Installation

①Click the button below

insert image description here
②Click Anaconda Prompt under Anaconda
insert image description here
③Enter the installation command in the following window, if it is divided into different environments, enter it first

activate 环境名

Then enter the installation statement, take the installation of the seaborn package as an example, install other packages and change the package name

pip install seaborn -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

Here is an example :
[Install the seaborn package in the pytorch environment I created] The
steps are as follows:
First: as shown in the figure, after clicking the Anaconda prompt, the following window appears, that is, the basic environment. I want to package it into the pytorch environment I created. , first enter

activate pytorch

insert image description here
After: Enter the pytorch environment
insert image description here
Next: Enter the statement of the installation package, taking seaborn as an example

insert image description here
Finally: Click Enter to install.

2.2 Uninstall

Steps 1 and 2 are the same as the installation, except that in step 3, the installation statement is replaced with uninstall (there is no need to add Douban source at this time) (the following is an example of uninstalling seaborn)

pip uninstall seaborn
2.3 Update

Steps 1 and 2 are the same as the installation, except that in step 3, replace the installation statement with upgrade (the following is an example of updating seaborn)

pip install --upgrade seaborn -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

Fourth, create different environments in Anaconda for pycharm to choose

①Repeat the steps [①][②] of the method [2] in [3], and call out the Anaconda prompt window, as shown in the figure
in the environment
② Check which versions of python are currently available for installation:

conda search --full-name python

insert image description here
③ Install different versions of python:
Take [the name of the created environment is pytorch, and the python version is 3.6] as an example (the environment name and python version number can be defined according to your own needs)

conda create --name pytorch python=3.6

Because I have already created it, I will not post pictures. After waiting, enter [y] to agree, so that you can create an environment with the environment name and python version number specified by yourself. In the [envs] under the Anaconda installation file You can see what environments you have in the folder.
insert image description here
You can see that this is the pytorch environment you just created. If you want to use this environment when programming pycharm, repeat the steps in [2], and in step [⑥] Select Anaconda—>envs—>pytorch—>python.exe to switch to the python compiler in the pytorch environment.
insert image description here
As shown in the figure, switch to the python compiler in the pytorch environment
insert image description here

5. Switching between different environments of Anaconda

In pycharm, python compilers in different environments are selected. Before installing the python package, activate the corresponding environment and then install the python package. There will be no problem that the package cannot be found after installing the package.
The following shows how to switch to each environment. In fact, it is very simple. Just use the activate statement. Take switching to the [pytorch] environment created by me as an example :

1. Click the Windows icon in the lower left corner of the computer desktop to open the Anaconda prompt

insert image description here
insert image description here

2. Enter the statement to switch the environment: activate + environment name

activate pytorch

As shown in the figure, it means that it has been switched to the pytorch environment.
insert image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324340124&siteId=291194637