Python Virtual Environment Setup (venv and uv)
Before starting quant work, first solve a problem no Python project can avoid: environment isolation.
This article introduces two lightweight tools - Python's built-in venv and the new blazing-fast tool uv - and uses them to prepare the environment for the quant course.
Why do we need virtual environments
Imagine a common scenario: a strategy script you wrote six months ago depends on pandas 1.x, while a tutorial you're learning today requires pandas 2.x, and the two cannot coexist in the same system environment.
Repeatedly installing and uninstalling different package versions directly in the system Python usually turns the environment into a tangled mess, commonly known as 'dependency hell'.
The idea behind virtual environments is simple: give each project its own independent package installation directory so they do not interfere with each other.
Quant projects especially need it - backtesting, machine learning and data scraping may each depend on different library versions, and a standalone quant environment ensures the code in later tutorials always runs as expected.
How the Three Options Divide the Work
There are three commonly used environment solutions in the Python ecosystem, each with a different positioning:
| Tool | Locate | Who It's For |
|---|---|---|
| venv | Built into Python, the most basic virtual environment solution | For all Python users, zero installation cost |
| uv | A new-generation tool with integrated management of environments, dependencies and Python versions, extremely fast | For developers pursuing efficiency, recommended in this article |
| Anaconda (conda) | A full-stack data science distribution with preinstalled scientific computing libraries and its own conda environment | For data science beginners, seeAnaconda Tutorial |
All three solve the same class of problem; in practice just pick one.
This article first explains the built-in venv to build the concepts, then the more efficient uv; the next article introduces Anaconda.
venv: Python's Built-in Solution
venv is the virtual environment module bundled with official Python; on Python 3.3 and above it can be used without installing anything.
Create a virtual environment
Run in the project directory:
Example
Create a virtual environment named quant-env
python3
-m
venv quant-env
If the command produces no output, the creation succeeded.
A quant-env folder appears in the current directory, containing an independent Python interpreter and package directory; all packages installed in this environment are stored here afterward.
Activation and Deactivation
A created environment must be 'activated' before use; the commands differ by platform:
| Platform | Activation Command |
|---|---|
| macOS / Linux (bash, zsh) | source quant-env/bin/activate |
| Windows (PowerShell) | .\quant-env\Scripts\Activate.ps1 |
| Windows (CMD) | quant-env\Scripts\activate.bat |
After successful activation, the environment name appears before the command prompt:
(quant-env) $ which python
/private/tmp/qt_venv_demo/quant-env/bin/python
(quant-env) $ python -V
Python 3.9.6
Note the output of which python: after activation the python command points to the interpreter inside the virtual environment, and all packages installed by pip go only into this environment.
Exiting the environment takes just one command:
(quant-env) $ deactivate
After exiting, the command prompt returns to normal and the python command goes back to the system environment.
Common pitfall: a virtual environment is tied to the directory, not the terminal - reactivate it when you switch terminal windows; to delete an environment just remove the quant-env folder, which does not affect the system Python.
Installing Dependencies and Reproducing Environments
After activating the environment, install packages with pip; it is recommended to also specify a domestic mirror to speed up downloads:
Example
Install a package: -i specifies the Tsinghua mirror, faster on domestic networks
pip
install
-i
https:
//
pypi.tuna.tsinghua.edu.cn
/
simple six
The output is shown below:
Collecting six
Using cached six-1.17.0-py2.py3-none-any.whl.metadata (1.7 kB)
Using cached six-1.17.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: six
Successfully installed six-1.17.0
Exporting the current environment's dependency list to requirements.txt is the standard way to share and reproduce an environment:
Example
Export the dependency list (package name == exact version)
pip freeze
requirements.txt
View the Contents
cat
requirements.txt
The output is shown below:
six==1.17.0
Once someone has this list, they can create a new environment and install all dependencies with a single command:
pip install -r requirements.txt
Limitations of venv
venv solves the 'isolation' problem, but has two everyday pain points:
First, it is slow: every new environment must re-download dependencies from the network, and the waiting time is significant when running multiple projects in parallel.
Second, it is bare: there is no dependency lock file (requirements.txt does not include dependencies of dependencies), no Python version management, and environment reproduction often drifts during team collaboration.
These pain points are exactly where uv, below, comes in.
uv: A New-Generation Blazing-Fast Solution
uv is a Python environment and package management tool written in Rust by Astral (the developer of the well-known tool Ruff).
It is compatible with pip usage habits while integrating virtual environments, dependency management and Python version management into one tool, far faster than traditional solutions.
Installing uv
On macOS / Linux, install with the official script:
curl -LsSf https://astral.sh/uv/install.sh | sh
On Windows, use the corresponding PowerShell script:
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
If Homebrew is installed, you can also simply:
brew install uv
After installation, verify the version:
$ uv --version
uv 0.10.0 (0ba432459 2026-02-05)
Creating Environments and Installing Dependencies
The command for uv to create a virtual environment is uv venv:
Example
Create a virtual environment named quant-uv-env
uv venv quant-uv-env
The output is shown below:
Using CPython 3.12.11
Creating virtual environment at: quant-uv-env
Activate with: source quant-uv-env/bin/activate
Note the first line: venv can only use the current system Python version, while uv automatically selects (and downloads if needed) a suitable Python version - equivalent to having Python version management built in.
Activation is the same as with venv; after activation use uv pip to install packages, with command habits consistent with pip:
Example
Activate the Environment
source
quant-uv-env
/
bin
/
activate
Install a package, compatible with pip's argument syntax
uv pip
install
--index-url
https:
//
pypi.tuna.tsinghua.edu.cn
/
simple six
The output is shown below:
Resolved 1 package in 6ms
Prepared 1 package in 442ms
Installed 1 package in 4ms
+ six==1.17.0
View installed packages:
$ uv pip list
Using Python 3.12.11 environment at: /tmp/qt_venv_demo/quant-uv-env
Package Version
------- -------
six 1.17.0
Where Exactly Does the Speed Come From
uv's speed advantage depends on the case; below is a local benchmark (macOS, Tsinghua mirror, installing pandas):
| Scenario | Time Taken | Description |
|---|---|---|
| pip first install | 17.1 seconds | Time is mostly spent downloading from the network |
| uv first install | 13.7 seconds | Also limited by network download, little advantage |
| uv second install (cache hit) | 0.1 seconds | uv has a global cache, so packages already installed skip the network |
The conclusion is clear: when limited by network downloads the two differ little; uv's essential advantage is the global cache - creating new environments and reinstalling dependencies is almost instantaneous.
The more projects and environments, the more obvious this advantage becomes.
Note: the table shows one benchmark run; absolute values vary with network conditions, but the order-of-magnitude relationship is stable. Especially in scenarios installing dependencies in bulk across many environments, uv's cache advantage is further amplified.
Project Management Mode
Besides acting as a 'faster pip', uv also provides a modern project management workflow based on pyproject.toml:
Example
Initialize a new project
uv init
--name
quant-demo
Add a dependency (automatically written to pyproject.toml and generates uv.lock)
uv add six
Run any command in the project environment
uv run python
-c
"import six; print('six version:', six.version)"
uv init generates a standard project structure in the current directory:
.git .gitignore .python-version main.py pyproject.toml README.md
Among them, pyproject.toml is the project's dependency declaration file, with the following initial content:
Example
File path: pyproject.toml
[project]
name = "quant-demo" # project name
version = "0.1.0" # project version
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12" # required Python version
dependencies = [] # dependency list, written automatically after uv add
After running uv add six, dependencies is automatically written as "six>=1.17.0", and the lock file uv.lock is generated to record each dependency's version precisely.
The output of uv add and uv run is shown below:
$ uv add six
Resolved 2 packages in 5ms
Prepared 1 package in 10.75s
Installed 1 package in 3ms
+ six==1.17.0
$ uv run python -c "import six; print('six 版本:', six.__version__)"
six 版本: 1.17.0
With pyproject.toml and uv.lock, a team member can restore a fully consistent environment with a single uv sync after getting the code - something venv + requirements.txt cannot do.
Tip: this tutorial only needs the compatible command pair uv venv plus uv pip to follow along; project management mode is for interested readers to go further.
How to Choose Between venv and uv
| Comparison Item | venv | uv |
|---|---|---|
| Whether installation is needed | Bundled with Python, zero installation | Needs separate installation (one command) |
| Environment creation and installation speed | Normal | Extremely fast, near-instant on a cache hit |
| Python version management | Can only use the current system version | Automatically selects and installs the required version |
| Dependency Locking | None (requirements.txt is incomplete) | uv.lock locks precisely |
| Learning Cost | Minimum | Low (compatible with pip habits) |
Advice for beginners: install uv on your system, use it as a faster pip, and the rest of the concepts are fully interchangeable with venv.
Hands-on: Prepare the Environment for the Quant Course
Use what you learned today to create a dedicated quant environment and install the libraries needed in later chapters. Pick either the venv or the uv command set:
Example
========== Option 1: venv ==========
python3
-m
venv quant-env
Create environment
source
quant-env
/
bin
/
activate
Activate (on Windows use Activate.ps1)
========== Option 2: uv ==========
uv venv quant-env
Create environment
source
quant-env
/
bin
/
activate
Activate
========== Common to Both Options Below ==========
Install basic quant libraries: data processing, numerical computing, visualization
uv pip
install
pandas numpy matplotlib
venv users just replace uv pip with pip
Install financial data and charting libraries (used in later chapters)
uv pip
install
yfinance akshare pyecharts
Verify the installation
python
-c
"import pandas, numpy, matplotlib, yfinance, akshare, pyecharts; print('Environment ready')"
The output is shown below:
环境就绪