Python virtual environment is very simple, after reading you will be

Hello everyone, my name is Cai.

Recently, some fans have asked about some operations in the Pythonvirtual environment , and it happens that the use of this aspect is usually involved, so let's briefly introduce it today.

1. Manage virtual environment based on conda

Since the author commonly used is Anaconda, so here is the first introduction based on condathe virtual environment operation.

Generally speaking, there are two ways to manage a virtual environment: one is visual operation and the other is command line operation.

1.1. Create a virtual environment

One is through the Anconada Navigatorvisual operation, select and Environmentsclick the bottom Createto enter the page for creating a virtual environment.

The second is to create through commands on the command line, and the several schemes introduced later are similar.

# 指令
conda create -n env1 python=3.8.8
复制代码

In the instruction, it env1is the name of the virtual environment, which can be customized; python=3.8.8it is the specified python version, which can be customized.

1.2. Activate the virtual environment

When the virtual environment is created, the following prompt will pop up:

done
#
# To activate this environment, use
#
#     $ conda activate env1
#
# To deactivate an active environment, use
#
#     $ conda deactivate
复制代码

These tips are actually good instructions for activating and exiting the virtual environment

# 激活虚拟环境
conda activate env1
复制代码

In the command, env1is the name of the virtual environment to be activated

激活虚拟环境env1

We can see that after the virtual environment is activated, the name of the virtual environment will be displayed at the front env1(marked in the red box in the above figure)

1.3. Manage third-party libraries for virtual environments

Only when we activate the specified virtual environment, we can manage the third-party library through pipor condadirectly.

安装第三方库

We can also use -nparameters to specify a virtual environment where third-party libraries need to be installed for cross-environment installation.

# env1是待安装第三方库的虚拟环境名称,numpy是待安装的第三方库
conda install -n env1 numpy
复制代码

跨环境安装第三方库

1.4. Exit the virtual environment

当我们激活了虚拟环境env1后,我们可以通过指令conda deactivate退出该虚拟环境并回到基础base环境

退出虚拟环境

1.5. 删除虚拟环境

# 删除虚拟环境
conda env remove -n env1
# 或者
conda remove -n env1 --all
复制代码

1.6. 更多操作指令

查看当前环境下已安装的第三方库

conda list
复制代码

查看指定环境下已安装的第三方库

conda list -n env1
复制代码

更新指定环境的第三方库

conda update -n env1 numpy
复制代码

删除指定环境的第三方库

conda remove -n env1 numpy
复制代码

查看全部虚拟环境

conda info -e
# 或者
conda env list
复制代码

前面带有符号*的是指当前环境

更新全部第三方库

conda update --all 
复制代码

2. 基于venv管理虚拟环境

venvPython标准库内置的虚拟环境管理工具,在python3.3之后登上舞台

# 创建虚拟环境(其中 F:\py_env\env2 是虚拟环境路径)
>>>python -m venv F:\py_env\env2

# 激活虚拟环境(运行 激活脚本)
>>>F:\py_env\env2\Scripts\activate

# 安装、更新、删除第三库方法同常规
>>>pip install plotly
>>>pip install --upgrade plotly
>>>pip uninstall plotly

# 退出虚拟环境
>>>deactivate

# 删除虚拟环境(最简单可以直接删除虚拟环境文件夹)
复制代码

关于这部分更详细的操作可以查阅python官方文档,参考地址:

https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/#creating-a-virtual-environment
复制代码

以上就是本次全部内容,欢迎大家留言交流~

Guess you like

Origin juejin.im/post/6998427531435212807