为什么 virtualenv(版本 20)比 python3 -m venv 快得多

why virtualenv (version 20) is much faster than python3 -m venv

我在我的 2015 macbook 上测试过,virtualenv 快了 6 倍。

virtualenv 版本为 20.0.21

$ time virtualenv $RANDOM
created virtual environment CPython3.8.3.final.0-64 in 340ms
  creator CPython3Posix(dest=/private/tmp/4997, clear=False, global=False)
  seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/Users/noname/Library/Application Support/virtualenv/seed-app-data/v1.0.1)
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

real    0m0.489s
user    0m0.269s
sys     0m0.283s
$ time /usr/local/opt/python@3.8/bin/python3 -m venv $RANDOM

real    0m3.112s
user    0m2.334s
sys     0m0.731s

python3.8 是通过自制软件安装的。

即使使用 --creator venv --always-copy 选项,virtualenv 也更快:

$ time virtualenv --creator venv --always-copy $RANDOM
created virtual environment CPython3.8.3.final.0-64 in 418ms
  creator Venv(dest=/private/tmp/28878, clear=False, global=False, describe=CPython3Posix)
  seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/Users/noname/Library/Application Support/virtualenv/seed-app-data/v1.0.1)
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

real    0m0.554s
user    0m0.316s
sys     0m0.309s

为什么他们的表现不一样?

Virtualenv 20.x 只是做了一些优化和小技巧,让它变得更快; venv 标准库模块没有这些,而且可能也不会得到它们。

据我了解,大部分差异是由于用于配置新创建的虚拟环境的方法不同所致。

2020 年 2 月发布了 virtualenv 的第 20 版,它是一个完全重写的版本。随之而来的是新概念 called seeders,它们定义了提供环境的不同方法,即制作诸如 pipsetuptools 在环境中可用。当前版本的 virtualenv 有两个播种器:

  • pip 这可能类似于 venvvirtualenv 的早期版本。
  • app-data,当前的默认播种器,它使用不同的机制,可能是速度改进的较大贡献者。

来自文档:

app-data - this method uses the user application data directory to create install images. These images are needed to be created only once, and subsequent virtual environments can just link/copy those images into their pure python library path (the site-packages folder). This allows all but the first virtual environment creation to be blazing fast (a pip mechanism takes usually 98% of the virtualenv creation time, so by creating this install image that we can just link into the virtual environments install directory we can achieve speedups of shaving the initial 1 minutes 10 seconds down to just 8 seconds in case of copy, or 0.8 seconds in case symlinks are available - this is on Windows, Linux/macOS with symlinks this can be as low as 100ms from 3+ seconds).

您还可以在此讨论中阅读更多技术细节: