在 Python 开发中,为不同的工程项目创建不同的开发环境是常见现象,这是因为不同项目可能需要不同的包版本依赖。除了非常好用的 conda 创建 Python 虚拟环境外,Python 自带有创建虚拟环境的方法,这种虚拟环境轻量,在一些项目中会用到,如 stable diffusion webui 等。本篇进行介绍。

venv 虚拟环境创建

假设服务器上 Pyhton 已经安装完成,且配置好环境变量。

1
2
3
4
5
6
7
8
# 先创建一个空目录,用来存放新的虚拟环境文件
mkdir -p /home/jinzhongxu/newenv

# 进入环境目录,创建虚拟环境
cd /home/jinzhongxu/newenv
python -m venv .
# 或者指定目录直接创建虚拟环境,而不需要进入目录
python -m venv /home/jinzhongxu/newenv

其实,创建的虚拟环境,就是把 Python 的一些可执行文件,如 pip, python 移动或创建软连接到该目录。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ ls /home/jinzhongxu/newenv
bin include lib lib64 pyvenv.cfg

$ cat /home/jinzhongxu/newenv/pyvenv.cfg
home = /usr/local/miniconda/bin
include-system-site-packages = false
version = 3.9.17

$ ls -l /home/jinzhongxu/newenv/bin
total 36
-rw-rw-r-- 1 jinzhongxu jinzhongxu 1900 8月 28 14:37 activate
-rw-rw-r-- 1 jinzhongxu jinzhongxu 849 8月 28 14:37 activate.csh
-rw-rw-r-- 1 jinzhongxu jinzhongxu 1989 8月 28 14:37 activate.fish
-rw-rw-r-- 1 jinzhongxu jinzhongxu 8834 8月 28 14:37 Activate.ps1
-rwxrwxr-x 1 jinzhongxu jinzhongxu 239 8月 28 14:37 pip
-rwxrwxr-x 1 jinzhongxu jinzhongxu 239 8月 28 14:37 pip3
-rwxrwxr-x 1 jinzhongxu jinzhongxu 239 8月 28 14:37 pip3.9
lrwxrwxrwx 1 jinzhongxu jinzhongxu 31 8月 28 14:37 python -> /usr/local/miniconda/bin/python
lrwxrwxrwx 1 jinzhongxu jinzhongxu 6 8月 28 14:37 python3 -> python
lrwxrwxrwx 1 jinzhongxu jinzhongxu 6 8月 28 14:37 python3.9 -> python

更多关于创建虚拟环境的介绍:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$ python -m venv --help
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear] [--upgrade]
[--without-pip] [--prompt PROMPT] [--upgrade-deps]
ENV_DIR [ENV_DIR ...]

Creates virtual Python environments in one or more target directories.

positional arguments:
ENV_DIR A directory to create the environment in.

optional arguments:
-h, --help show this help message and exit
--system-site-packages
Give the virtual environment access to the system site-packages dir.
--symlinks Try to use symlinks rather than copies, when symlinks are not the default
for the platform.
--copies Try to use copies rather than symlinks, even when symlinks are the default
for the platform.
--clear Delete the contents of the environment directory if it already exists,
before environment creation.
--upgrade Upgrade the environment directory to use this version of Python, assuming
Python has been upgraded in-place.
--without-pip Skips installing or upgrading pip in the virtual environment (pip is
bootstrapped by default)
--prompt PROMPT Provides an alternative prompt prefix for this environment.
--upgrade-deps Upgrade core dependencies: pip setuptools to the latest version in PyPI

Once an environment has been created, you may wish to activate it, e.g. by sourcing an activate
script in its bin directory.

激活虚拟环境

想要在当前终端使用该虚拟环境,需要先激活该虚拟环境:

1
source /home/jinzhongxu/newenv/bin/activate

此时,会在提示符增加该虚拟环境的标识:(newenv)。接下来在此终端进行的所有 Python 操作,如安装包、删除包、执行 Python 程序等都是在该虚拟环境中进行。安装包会安装在当前虚拟环境目录的 /home/jinzhongxu/newenv/lib/python3.9/site-packages.

退出虚拟环境

如果不想在该终端上继续使用该虚拟环境,那么可以使用如下的命令退出该虚拟环境:

1
deactivate

删除虚拟环境

删除该虚拟环境,首先先退出虚拟环境,然后删除虚拟环境目录即可。

1
2
deactivate
rm -rf /home/jinzhongxu/newenv

参考文献

  1. venv
  2. venv — 创建虚拟环境