Files
nexus/wiki/concepts/虚拟环境-venv.md
2026-04-18 12:03:16 +08:00

30 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "虚拟环境 (venv)"
type: concept
tags: [python, 环境配置]
---
## 定义
Python 虚拟环境Virtual Environment是一种依赖隔离机制通过 `python3 -m venv venv` 命令创建独立的 Python 运行环境,使每个项目拥有独立的依赖包,避免全局污染和版本冲突。
## 核心特性
- 依赖隔离:每个项目可安装不同版本的同一包
- 环境激活:`source venv/bin/activate` 激活环境
- 可移植性:通过 `pip freeze > requirements.txt` 导出依赖列表
- 轻量级:基于 Python 标准库实现,无需额外安装
## 使用场景
- Docker 容器内运行 Python 应用
- 多项目共存且依赖版本不同
- 隔离系统 Python 环境,避免全局污染
## 相关工具
- [[venv]]Python 3.3+ 内置模块
- [[virtualenv]]:第三方虚拟环境工具,功能更丰富
- [[pipenv]]:结合 pip 和 venv 的依赖管理工具
- [[poetry]]:现代化的 Python 依赖管理工具
## 关联概念
- [[Docker]]容器化部署环境venv 常用于隔离容器内 Python 依赖
- [[Scrapy]]Python 爬虫框架,可在 venv 中运行
- [[Playwright]]:浏览器自动化工具,可在 venv 中运行