Understanding Virtual Environments#
Introduction#
In larger projects, it’s crucial to have a separate Python environment for each feature or branch you work on. This practice ensures isolation and reproducibility, simplifying testing and debugging by allowing issues to be traced back to specific environments. It also facilitates smoother integration and deployment processes, ensuring controlled and consistent development. Managing multiple environments might seem daunting, but it’s straightforward with the right tools. Follow the steps below to set up and manage your environments efficiently.
Benefits#
Isolation: Avoids conflicts between dependencies of different features.
Reproducibility: Each environment can be easily replicated.
Simplified Testing: Issues are contained within their respective environments.
Smooth Integration: Ensures features are developed in a consistent setting.
Prerequisites#
Make sure that you have uv installed. This can be done system-wide with pipx or within a conda environment. Note that we use conda only for setting up the interpreter (Python version) and uv, not for managing dependencies.
Refer to the official development guide for detailed instructions:
https://docs.flexcompute.com/projects/tidy3d/en/stable/development/index.html#installation
Setting Up a New Environment#
Check out the branch:
git checkout branch
Set up the environment with
conda(skip this step if you don’t useconda):conda create -n branch_env python=3.11 conda activate branch_env python --version
Install dependencies with
uv:uv sync --active --frozen --extra dev uv run pre-commit install
Update the environment when switching to a different branch:
uv sync --active --frozen --extra dev
Multiple Folders or Worktrees#
If you have multiple folders (e.g., multiple clones or git worktrees), you will need to repeat the environment setup for each folder. Ensure that each folder has its own isolated environment.
By following these steps, you can maintain isolated and reproducible environments for each branch and feature, leading to a more efficient and error-free development process.