A Python project may need some dependent packages. There are multiple ways to allow projects with conflicting package configuration to run. The central idea is to set up environments that you can switch among each other and pick the one appropriate for the project.
Python has built-in virtualenv support. But there are also uv and
pyenv-virtualenv to create environments.
uv, pyenv, and pip-tools
Four tools are in concern here:
- uv: It manages Python project dependencies, lockfiles, virtual environments, and command execution. It can also install Python versions.
- pyenv: Installs and switches between Python versions, globally or per project.
- pyenv-virtualenv: A pyenv plugin that creates and activates isolated environments, each with its own installed packages.
- pip-tools: Locks and synchronizes package dependencies. Also generates pinned
requirements files (
pip-compile), or makes an existing environment match them (pip-sync).
uv keeps the Python in ~/.local/share/uv/python while pyenv keeps it in
~/.pyenv/versions. The virtual environment managed by uv is per-project under
.venv/. However, pyenv-virtualenv creates a centralized environment under
~/.pyenv/envs/. Per-project virtual environments in uv does not mean duplicated
storage. It keeps a shared package cache under ~/.cache/uv and packages installed in a
virtual environment are cloned from it, using copy-on-write or hard links.
Both uv and pip-tools can manage an environment specification: uv uses a lockfile,
while pip-tools uses pip-compatible requirements file.
uv vs pyenv-virtualenv + pip-tools
uv is a all-in-one tool. Its features can be replaced by pyenv + pyenv-virtualenv +
pip-tools. Both approaches can manage a reproducible development environment. The table
below compares them:
| Purpose | uv | pyenv-virtualenv + pip-tools |
|---|---|---|
| Select Python version | .python-version |
.python-version |
| Declare application dependencies | pyproject.toml |
Same pyproject.toml |
| Declare optional browser/OCR dependencies | [project.optional-dependencies] |
Same |
| Declare development dependencies | [dependency-groups] |
Same, with an export step |
| Record resolved package versions | uv.lock |
Generated requirements/*.txt |
| Store installed environment | Project-local .venv/ |
Named environment under pyenv’s directories |
This abbreviated example illustrates the format of dependency declarations in pyproject.toml:
[project]
dependencies = [
"pypdf>=5.0",
"pymarc>=5.2",
]
[project.optional-dependencies]
browser = ["playwright>=1.49"]
ocr = ["pypdfium2>=4.30", "pillow>=11"]
[dependency-groups]
dev = ["pytest>=8.3", "ruff>=0.11"]
A uv.lock entry is structured TOML, with package versions, dependency
relationships, sources and artifact hashes. For example:
[[package]]
name = "pypdf"
version = "7.2.0"
source = { registry = "https://pypi.org/simple" }
A generated pip-tools file uses requirements syntax:
pypdf==7.2.0
How to use pyenv and pip-tools
There are two components to install, pyenv and pyenv-virtualenv. Since it is a shell script solution, you need to configure your shell to use them:
eval "$(pyenv init - zsh)" # or "bash"
eval "$(pyenv virtualenv-init -)"
Setup
To install a Python version:
# show all available versions
pyenv install -l
# install a particular version
pyenv install 3.13.13
# show the installed versions
pyenv versions
In pyenv, install a version means to build CPython from source. You should have the build prerequisites installed first.
To create a virtual environment:
# create with a particular Python version
pyenv virtualenv 3.13.13 my_env
# show the created environments
pyenv virtualenvs
# switch to the environment
pyenv activate my_env
# show the activated environment
pyenv version
# switch back to the system Python
pyenv deactivate
In a particular project, you can pin a particular environment. You will be switched to that environment when you are in the project directory.
pyenv local my_env
Under the hood, it writes the environment name into .python-version.
Package management
In pyenv, you use the pip command to install packages after you have activated the environment.
pyenv activate my_env
# either of the following commands will work
python -m pip install requests
pip install requests
# this works without activating the environment
"$(pyenv root)/versions/my_env/bin/python" -m pip install requests
However, an environment is not associated with a project. Project dependencies is specified in pyproject.toml. For example:
[project]
name = "This-Project"
version = "0.1.0"
dependencies = [
"pypdf>=5.0",
"pymarc>=5.2",
]
[project.optional-dependencies]
browser = ["playwright>=1.49"]
ocr = ["pypdfium2>=4.30", "pillow>=11"]
[dependency-groups]
dev = ["pytest>=8.3", "ruff>=0.11"]
This setup allows you to install packages with options (the -e flag means editable mode):
# install the core dependencies
pip install -e .
# install the browser and OCR dependencies
pip install -e '.[browser,ocr]'
# install the development dependencies
pip install -e '.[dev]'
Recording the current environment is better to use pip-tools:
pyenv activate my_env
pip install pip-tools
mkdir -p requirements
then you can create multiple requirements files, such as requirements/dev.in, with the content:
pytest>=8.3
ruff>=0.11
The requirements files can also be generated using the piptools compile command, but exact versions will be recorded:
# Core application dependencies.
python -m piptools compile --generate-hashes --strip-extras \
-o requirements/core.txt pyproject.toml
# Core plus browser.
python -m piptools compile --generate-hashes --strip-extras --extra browser \
-o requirements/browser.txt pyproject.toml
# Full runtime.
python -m piptools compile --generate-hashes --strip-extras --all-extras \
-o requirements/full.txt pyproject.toml
# Full runtime plus development tools.
python -m piptools compile --generate-hashes --strip-extras --all-extras \
-o requirements/dev.txt pyproject.toml requirements/dev.in
# Upgrade one package at a later time, when pyproject.toml is changed
python -m piptools compile --generate-hashes --strip-extras --all-extras \
--upgrade-package pypdf \
-o requirements/dev.txt pyproject.toml requirements/dev.in
# Upgrade all packages
python -m piptools compile --generate-hashes --strip-extras --all-extras \
--upgrade \
-o requirements/dev.txt pyproject.toml requirements/dev.in
With these, you can synchronize the environment with the requirements files:
python -m piptools sync requirements/dev.txt --pip-args="--require-hashes"
The sync installs, upgrades and removes packages to match the selected file.
You may need to run pip install --no-deps -e . afterwards to install the project itself without resolving runtime dependencies.
Running scripts under pyenv
Since pyenv allows multiple Python versions and environments coexist in the system, you can run scripts under a specific environment even without activating it first.
There are two ways to do this:
# Set the environment variable and run the script
PYENV_VERSION=my_env pyenv exec python my_script.py
# Run the script with a specific Python interpreter
pyenv_root="${PYENV_ROOT:-$HOME/.pyenv}"
exec "$pyenv_root/versions/my_env/bin/python" my_script.py
How to use uv
It is the best if uv is installed globally. You can follow the official installation instructions.
Setup
You can install the prebuilt Python distributions in uv, which usually stored in ~/.local/share/uv/python:
# Show installed and available versions
uv python list
# Install a Python release series
uv python install 3.13
# Or install an exact version
uv python install 3.13.13
# Find the installation directory
uv python find 3.13.13
# Show the installation directory
uv python dir
In a project, you can pin a particular Python version:
uv python pin 3.13.13
This writes .python-version, but does not declare package dependencies. When you pin a version, you will use that version when you run a script with uv run.
In uv, you create a virtual environment at a particular directory (usually under a project directory):
# Create .venv/ locally, using the default Python version
uv venv
# Create .venv with a specific Python version
uv venv --python 3.13.13
# Activate the environment
source .venv/bin/activate
# Deactivate
deactivate
Package management
When you start a new project, you can initialize pyproject.toml and uv.lock with:
# Create a scaffold project in the local directory
uv init
# Create a minimal setup without scaffold
uv init --bare
A scaffold project will include README.md and src/project_name/__init__.py.
With pyproject.toml, you can declare project dependencies as mentioned above.
You can also add or remove dependencies to pyproject.toml using uv add or uv remove:
# Add a dependency
uv add 'requests>=2.32'
uv add --group dev ruff
uv add --optional browser beautifulsoup4
# Remove a dependency, use --group or --optional if applicable
uv remove requests
These update the appropriate pyproject.toml declaration, resolve uv.lock,
and synchronize the environment.
You can also install packages directly into a chosen environment without updating pyproject.toml or uv.lock:
# install to current virtual environment
uv pip install requests
# install to a specific virtual environment
uv pip install --python /path/to/.venv/bin/python requests
The uv.lock file describes the exact packages for a project while pyproject.toml declares the dependencies. You can update uv.lock after you edited pyproject.toml:
# update `uv.lock` only if something is incompatible
uv lock
# update `uv.lock` by resolving newer versions
uv lock --upgrade
# update `uv.lock` by resolving newer versions of a particular package
uv lock --upgrade-package pypdf
This does not install the packages to the virtual environment. To update uv.lock and install the packages together, you use uv sync:
# update `uv.lock` if needed and install the packages to the virtual environment
uv sync
# update `uv.lock` and install packages, with optional extras
uv sync --extra browser
uv sync --all-extras
# install packages without updating `uv.lock`, fail if `uv.lock` is out of date
uv sync --locked
# update `uv.lock` by resolving newer versions, then install the packages
uv sync --upgrade
Running scripts under uv
You can run a script without activating the corresponding virtual environment:
# run using uv
uv run --project /path/to/project --locked script.py
# run using a specific virtual environment
/path/to/.venv/bin/python script.py
The first command will select the .venv under the selected project, even if a different environment is activated. The --locked asks uv not to update the lockfile.