一、为何要虚拟环境?

项目依赖隔离:公司正在开发项目A,需要的python3.10版本,同时,马上因上线项目B,需要python3.12版本。

保持系统整洁:无需在本体操作系统用pip install安装软件包,避免污染全局级python环境。

便于依赖管理和项目部署:可以很轻松的生成项目所需要的所有依赖包列表(requirements.txt)

二、虚拟环境部署优缺点

工具名称 类型 Python版本支持 安装方式 特点 适用场景
venv 内置模块 ≥ 3.3 无需安装,内置 轻量、易用、官方强推 一般开发、日常运维
virtualenv 第三方工具 2.x 和 3.x pip install virtualenv 老牌、2.x与3.x通杀 兼容性广,功能比venv丰富
conda 第三方工具 2.x 和 3.x 随 Anaconda/Miniconda 一起安装 跨语言包管理(python/R/C/C++)、数据科学、机器学习 数据科学、机器学习

三、venv部署

3.1、基本语法

python3 -m venv 环境名称

3.2、创建项目目录

mkdir -p /data/MyProject && /data/MyProject

#注意虚拟环境一旦创建,不要移动到其它目录(可用新建代替移动)

3.3、创建名为 .venv 的虚拟环境目录

python -m venv .venv

[root@liuguohua MyProject]# tree .venv/ -L 2
.venv/
├── bin
│   ├── activate
│   ├── activate.csh
│   ├── activate.fish
│   ├── easy_install
│   ├── easy_install-3.6
│   ├── pip
│   ├── pip3
│   ├── pip3.6
│   ├── python -> python3
│   └── python3 -> /usr/bin/python3
├── include
├── lib
│   └── python3.6
├── lib64 -> lib
└── pyvenv.cfg

3.4、激活虚拟环境 (Linux/macOS)

source .venv/bin/activate

激活成功后,命令行提示符通常会显示环境名称
(.venv) [root@localhost MyProject]

3.5、在虚拟环境中操作

# 安装包
(.venv) [root@liuguohua MyProject]# pip install django==3.2.20
Collecting django==3.2.20
  Downloading https://files.pythonhosted.org/packages/84/eb/5329ae72bf26b91844985d0de74e4edf876e3ca409d085820f230eea2eba/Django-3.2.20-py3-none-any.whl (7.9MB)
    100% |████████████████████████████████| 7.9MB 290kB/s 
Collecting asgiref<4,>=3.3.2 (from django==3.2.20)
  Downloading https://files.pythonhosted.org/packages/fe/66/577f32b54c50dcd8dec38447258e82ed327ecb86820d67ae7b3dea784f13/asgiref-3.4.1-py3-none-any.whl
Collecting pytz (from django==3.2.20)
  Downloading https://files.pythonhosted.org/packages/81/c4/34e93fe5f5429d7570ec1fa436f1986fb1f00c3e0f43a589fe2bbcd22c3f/pytz-2025.2-py2.py3-none-any.whl (509kB)
    100% |████████████████████████████████| 512kB 4.6MB/s 
Collecting sqlparse>=0.2.2 (from django==3.2.20)
  Downloading https://www.liuguohua.com/packages/98/5a/66d7c9305baa9f11857f247d4ba761402cea75db6058ff850ed7128957b7/sqlparse-0.4.4-py3-none-any.whl (41kB)
    100% |████████████████████████████████| 51kB 2.9MB/s 
Collecting typing-extensions; python_version < "3.8" (from asgiref<4,>=3.3.2->django==3.2.20)
  Downloading https://files.pythonhosted.org/packages/45/6b/44f7f8f1e110027cf88956b59f2fad776cca7e1704396d043f89effd3a0e/typing_extensions-4.1.1-py3-none-any.whl
Installing collected packages: typing-extensions, asgiref, pytz, sqlparse, django
Successfully installed asgiref-3.4.1 django-3.2.20 pytz-2025.2 sqlparse-0.4.4 typing-extensions-4.1.1
You are using pip version 9.0.3, however version 25.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.


#查看已安装的包
(.venv) [root@localhost MyProject]# pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
asgiref (3.4.1)
Django (3.2.20)
pip (9.0.3)
pytz (2025.2)
setuptools (39.2.0)
sqlparse (0.4.4)
typing-extensions (4.1.1)
You are using pip version 9.0.3, however version 25.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.


导出依赖
(.venv) [root@localhost MyProject]# pip freeze > requirements.txt


(.venv) [root@localhost MyProject]# more requirements.txt 
asgiref==3.4.1
Django==3.2.20
pytz==2025.2
sqlparse==0.4.4
typing-extensions==4.1.1

3.6、包安装加速

语法:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple package_name 

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests

3.7、升级pip

python -m pip install --upgrade pip

3.8、退出虚拟环境

(.venv) [root@localhost MyProject]# deactivate

3.9、删除虚拟环境

确认退出虚拟环境后,删除隐藏目录即可
rm -rf .venv   #Linux/macOS

四、virtualenv部署

virtualenvvenv 的前身,在 venv 出现之前是事实上的标准。它支持 Python 2 和 Python 3。

4.1、安装

# pip3 install virtualenv

4.2、创建虚拟环境

[root@liuguohua MyProject2]# mkdir MyProject2 && cd MyProject2

[root@localhost MyProject2]# virtualenv MyEnv

[root@localhost MyProject2]# tree MyEnv/ -L 2
MyEnv/
├── bin
│   ├── activate
│   ├── activate.csh
│   ├── activate.fish
│   ├── activate.nu
│   ├── activate.ps1
│   ├── activate_this.py
│   ├── pip
│   ├── pip3
│   ├── pip-3.6
│   ├── pip3.6
│   ├── python -> /usr/bin/python3.6
│   ├── python3 -> python
│   ├── python3.6 -> python
│   ├── wheel
│   ├── wheel3
│   ├── wheel-3.6
│   └── wheel3.6
├── lib
│   └── python3.6
├── lib64
│   └── python3.6
└── pyvenv.cfg

5 directories, 18 files

4.3、激活环境

[root@liuguohua MyProject2]# source MyEnv/bin/activate
(MyEnv) [root@localhost MyProject2]# 

五、conda部署

5.1、安装

https://repo.anaconda.com/archive/     #官网,文件包大,1G左右
https://repo.anaconda.com/miniconda/   #官网,文件包小,100M左右

[root@localhost data]# mkdir MyProject3 && cd MyProject3

[root@localhost data]# wget https://repo.anaconda.com/archive/Anaconda3-2025.06-1-Linux-x86_64.sh

[root@localhost data]# sh Anaconda3-2025.06-1-Linux-x86_64.sh #会安装一个新的python3版本

Welcome to Anaconda3 2025.06-1

In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>> 
By continuing installation, you hereby consent to the Anaconda Terms of Service available at https://anaconda.com/legal.


Do you accept the license terms? [yes|no]
>>> yes

Anaconda3 will now be installed into this location:
/root/anaconda3

  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below

[/root/anaconda3] >>> 
PREFIX=/root/anaconda3
Unpacking payload ...
entry_point.py:256: DeprecationWarning: Python 3.14 will, by default, filter extracted tar archives and reject files or modify their metadata. Use the filter argument to control this behavior.

初始化
[root@localhost data]# ~/anaconda3/bin/conda init

#禁用每次打开shell终端就自动激活conda环境
(base) [root@localhost ~]# conda config --set auto_activate_base false
WARNING conda.cli.main_config:_set_key(453): Key auto_activate_base is an alias of auto_activate; setting value with latter

设置环境变量
[root@liuguohua data]# vi ~/.bashrc 
export PATH=~/anaconda3/bin:$PATH

[root@localhost data]# source ~/.bashrc

查版本
[root@localhost data]# conda -V
conda 25.5.1

5.2、使用

#默认有一个base虚拟环境
[root@localhost MyProject3]# conda activate base
(base) [root@localhost MyProject3]# python -V
Python 3.13.5

#创建新的虚拟环境
[root@localhost MyProject3]# conda create -n MyEnv python=3.9

#激活环境
[root@localhost MyProject3]# conda activate MyEnv


(MyEnv) [root@liuguohua MyProject3]# python -V
Python 3.9.23

#停用环境
conda deactivate
声明:欢迎大家光临本站,学习IT运维技术,转载本站内容,请注明内容出处”来源刘国华教育“。如若本站内容侵犯了原著者的合法权益,请联系我们进行处理。