Python creates a virtual environment (1): Use conda to create a virtual environment

1. Create a virtual environment

Open the cmd terminal and enter the command: python to view the Python version installed by yourself. It
Insert picture description here
can be seen that the python version installed in my system is 3.7.4. Please also check the python version in your system and perform the following operations to create a virtual environment:

Enter the command: conda create -n pachong python==3.7.4

This command creates a virtual environment named pachong, based on python version 3.7.4 ( determined according to the python environment in your system!!! ).

Note : The
virtual environment created by conda is different from the virtual environment created by pip. The virtual environment created by pip will directly generate a virtual environment directory in the current directory. To activate the virtual environment, you also need to specify this directory. The virtual environment installed by conda will only generate the directory of the virtual environment in the env directory under the anaconda installation directory . As shown below: the
Insert picture description here
newly created "crawler" virtual environment is in the env directory under my anaconda installation directory.

2. Activate the virtual environment

Enter in the terminal: source activate environment name to activate the virtual environment.

If an error message is reported after entering "source activate environment name" :
Insert picture description here
just delete "source":

Insert picture description here
As shown in the figure above, it means that the environment is successfully activated. The (pachong) in the left bracket means that you are currently in the pachong virtual environment that has just been created and activated.

3. View all virtual environments in the conda environment

Use the command: conda info --envs
Insert picture description here

4. Install additional packages for the virtual environment

Use the command: conda install -n environment name package name

An first checks the existing packages in the environment: Insert picture description here
install package bs4:
enter the command: conda install -n pachong bs4 and
check the existing packages in the environment again after the installation is complete: it is
Insert picture description here
found that the bs4 package is added, indicating that the installation is successful.

If you install the package for the current environment in the current environment, you don't need to specify the environment name, just use: conda install package name.

5. Set up a domestic mirror

When installing the package, I found that the speed is very slow, because the Anaconda.org server is abroad, but we can configure the domestic mirror for anaconda:
common mirror:
http://mirrors.aliyun.com/pypi/simple/ //Ali https ://pypi.tuna.tsinghua.edu.cn/simple/ //Tsinghua
http://pypi.douban.com/ //douban
http://pypi.hustunique.com/ //Huazhong University of Science and Technology
http:// pypi.sdutlinux.org/ //Shandong University of Technology
http://pypi.mirrors.ustc.edu.cn/ // University of Science and Technology of China
Insert picture description here
As shown in the picture above, the mirror image is successfully set to Douban.

6. Close the virtual environment

conda deactivate

Insert picture description here
You can find that there is no "(pachong)" on the left, which means you have successfully exited.

7. Delete the virtual environment

conda remove -n environment name --all
before deleting: it
Insert picture description here
Insert picture description here
Insert picture description here
can be seen that pachong has been deleted.

Guess you like

Origin blog.csdn.net/qq_45154565/article/details/109326095