Logo
活死人の行知路

Python虚拟环境配置


📅 | 📝 584 字
#python

1虚拟环境安装与配置

为什么需要虚拟环境?

Python 的虚拟环境与系统之间相互隔离,一个项目一个虚拟环境,不同的虚拟环境不会相互影响,最大程度避免包冲突。缺点就是会占用更大的空间。

参考

1.1virtualenvwrapper-win

这是一款在 Windows 下使用的虚拟环境工具。

  1. 首先使用下面命令进行安装,安装完之后会在 Python 的可执行路径下安装一个 virtualenvexe 文件。
pip install virtualenvwrapper-win
  1. 使用 mkvirtualenv 命令创建虚拟环境,安装时会默认基于 Python 默认版本去新建一个虚拟环境。
mkvirtualenv python_start
  1. 删除虚拟环境
rmvirtualenv python_start
  1. 创建的虚拟环境是默认 python 版本,也可以通过 -p 参数指定其他 Python 版本在创建虚拟环境:
mkvirtualenv -p {Python Path}\python.exe python_start

1.2virtualenv

这是一款可以用于 MacOS/Linux 环境中的虚拟环境工具。

  1. 安装。
pip3 install virtualenv -i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install virtualenvwrapper -i https://pypi.tuna.tsinghua.edu.cn/simple

# OR
pip3 install virtualenv -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn
pip3 install virtualenvwrapper -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn
  1. 配置。

找到 virtualenvwrapper.sh 文件。这里不同的 Linux 系统 virtualenvwrapper.sh 路径可能不一致,最好通过 find 命令查询一下。

# CentOS
find / -name 'virtualenvwrapper.sh'
/usr/local/python3.13/bin/virtualenvwrapper.sh

# MacOS
/Library/Frameworks/Python.framework/Versions/3.13/bin/virtualenvwrapper.sh

编辑 .bashrc 文件:

vim ~/.bashrc

在最后添加下面内容:

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/python3.13/bin/virtualenv
source /usr/local/python3.13/bin/virtualenvwrapper.sh
  • VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3:表示当使用 virtualenv 去新建 python 虚拟环境的时候会到 /usr/bin/python3 位置去找 python 的可执行文件。

最后执行下面命令重新加载:

source  ~/.bashrc
  1. 创建虚拟环境
# mkvirtualenv python_start
created virtual environment CPython3.13.0.final.0-64 in 153ms
  creator CPython3Posix(dest=/root/.virtualenvs/python_start, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
    added seed packages: pip==24.3.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
virtualenvwrapper.user_scripts creating /root/.virtualenvs/python_start/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/python_start/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/python_start/bin/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/python_start/bin/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/python_start/bin/get_env_details
(python_start) [root@chaos-3 Python-3.13.0]# python
Python 3.13.0 (main, Nov  4 2024, 04:09:22) [GCC 8.5.0 20210514 (Red Hat 8.5.0-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
  1. 虚拟环境目录
(python_start) [root@chaos-3 opt]# ls ~/.virtualenvs/
get_env_details  postactivate	 postmkproject	   postrmvirtualenv  predeactivate  premkvirtualenv  python_start
initialize	 postdeactivate  postmkvirtualenv  preactivate	     premkproject   prermvirtualenv

其中 python_start 就是虚拟环境的地址。

  1. 虚拟环境常用命令
# 创建环境名
mkvirtualenv 环境名
# 退出环境
deactivate
# 进入环境
workon 环境名
# 删除环境
rmvirtualenv 环境名
# 列出所有环境
lsvirtualenv

1.3venv

  1. 创建一个新的虚拟环境时,选择 Python 解释器并创建一个 ./venv 目录来存放它:
# Linux or MacOS
python3 -m venv --system-site-packages ./venv
# Windows
python -m venv --system-site-packages .\venv
  1. 使用特定于 shell 的命令激活该虚拟环境:
# sh, bash, or zsh
source ./venv/bin/activate  
# csh or tcsh
source ./venv/bin/activate.csh  
# csh or tcsh
source ./venv/bin/activate.csh  

当虚拟环境处于有效状态时,shell 提示符带有 (venv) 前缀。 在不影响主机系统设置的情况下,在虚拟环境中安装软件包。首先升级 pip:

pip3 install --upgrade pip
pip3 list  # show packages installed within the virtual environment
  1. 退出虚拟环境:
deactivate  # don't exit until you're done using TensorFlow
  1. 示例操作
Envs python3 -m venv --system-site-packages ./cat-srvs
➜  Envs source ./cat-srvs/bin/activate
(cat-srvs) ➜  Envs pip list
Package          Version
---------------- ---------
certifi          2020.12.5
chardet          4.0.0
idna             2.10
jsonrpclib-pelix 0.4.2
pip              21.0.1
requests         2.25.1
setuptools       49.2.1
urllib3          1.26.3
WARNING: You are using pip version 21.0.1; however, version 21.1.3 is available.
You should consider upgrading via the '/Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9 -m pip install --upgrade pip' command.
(cat-srvs) ➜  Envs pip install --upgrade pip
Requirement already satisfied: pip in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (21.0.1)
Collecting pip
  Downloading pip-21.1.3-py3-none-any.whl (1.5 MB)
     |████████████████████████████████| 1.5 MB 349 kB/s
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 21.0.1
    Uninstalling pip-21.0.1:
      Successfully uninstalled pip-21.0.1
Successfully installed pip-21.1.3
(cat-srvs) ➜  Envs pip list
Package          Version
---------------- ---------
certifi          2020.12.5
chardet          4.0.0
idna             2.10
jsonrpclib-pelix 0.4.2
pip              21.1.3
requests         2.25.1
setuptools       49.2.1
urllib3          1.26.3
(cat-srvs) ➜  Envs deactivate
➜  Envs
  1. 删除虚拟环境,只需要删除项目目录下的虚拟环境目录即可。

1.4uv

uv 是 Astral 公司开发的极快的 Python 包管理和项目管理工具(用 Rust 编写),它可以替代 virtualenv 来创建虚拟环境。

创建虚拟环境的基本命令

  1. 在项目目录下运行:
uv venv
  • 这会在当前目录下创建一个名为 .venv 的虚拟环境(默认名称)。
  • 如果需要指定自定义目录名,例如 myenv,执行 uv venv myenv
  • 如果你想使用特定 Python 版本创建环境,执行 uv venv --python 3.12
  • uv 会自动下载和管理所需的 Python 版本(如果系统中没有),支持如 3.12.0pypy@3.10 等精确指定。

包含 seed 包(可选) 默认情况下,uv 创建的虚拟环境不包含 pip、setuptools 和 wheel(因为 uv 自己处理依赖)。如果需要兼容传统工具(如某些 IDE 或 Jupyter),添加 --seed

uv venv --seed
  1. 激活虚拟环境
# macOS/Linux
source .venv/bin/activate

# Windows PowerShell
.venv\Scripts\Activate.ps1
# Windows CMD
.venv\Scripts\activate.bat

激活后,你可以用 uv pip install 或直接 uv add(在项目中)安装包。

  1. 示例完整流程:
# 创建环境(默认 .venv)
uv venv

# 激活(Linux/macOS 示例)
source .venv/bin/activate

# 安装包
uv pip install requests

# 运行脚本
python your_script.py

uv 创建虚拟环境的速度远超 python -m venv,推荐在项目中使用 uv init 初始化项目,它会自动处理环境和依赖。

更多详情可查看官方文档:https://docs.astral.sh/uv/

2pip配置

更新pip版本到最新

pip3 install --upgrade pip

查看当前源

➜  pip3 config list
global.index-url='https://pypi.tuna.tsinghua.edu.cn/simple'
global.trusted-host='pypi.tuna.tsinghua.edu.cn'

镜像加速配置

# 配置
pip3 config set global.index-url https://mirrors.cloud.tencent.com/pypi/simple
pip3 config set global.trusted-host mirrors.cloud.tencent.com

3更新日志

  • 2024.11.04 更新 Python 版本为 3.13。
  • 2025.09.29 更新 Python Pip 镜像加速配置。