|
| 1 | +# Setting Up Your Environment |
| 2 | + |
| 3 | +A guide to getting your development environment set up for working with Tools for Experiments. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Option 1: Using uv |
| 8 | + |
| 9 | +### What is uv? |
| 10 | + |
| 11 | +[uv](https://docs.astral.sh/uv/) is a fast Python package and project manager written in Rust, developed by Astral (the team behind `ruff`). It serves as a drop-in replacement for `pip`, `pip-tools`, `virtualenv`, and more — but orders of magnitude faster. |
| 12 | + |
| 13 | +Key concepts: |
| 14 | +- **`pyproject.toml`**: The single source of truth for your project's dependencies and metadata, following the modern Python packaging standard. |
| 15 | +- **`uv.lock`**: A lockfile automatically generated by uv that pins exact versions of all dependencies (direct and transitive) for reproducible environments. |
| 16 | +- **Editable installs**: uv supports installing local packages in editable mode (`editable = true`), meaning changes to the source code of those packages are immediately reflected without reinstalling. |
| 17 | +- **Virtual environments**: uv automatically creates and manages a `.venv` in your project directory. |
| 18 | + |
| 19 | +### Installing uv |
| 20 | + |
| 21 | +```bash |
| 22 | +curl -LsSf https://astral.sh/uv/install.sh | sh |
| 23 | +``` |
| 24 | + |
| 25 | +Or via pip: |
| 26 | + |
| 27 | +```bash |
| 28 | +pip install uv |
| 29 | +``` |
| 30 | + |
| 31 | +### Project structure: `pyproject.toml` |
| 32 | + |
| 33 | +Each measurement or experiment folder should have a `pyproject.toml` that declares its dependencies. For packages that live locally on your machine (like `instrumentserver` or `labcore`), use `[tool.uv.sources]` to point uv to their local paths with editable installs: |
| 34 | + |
| 35 | +```toml |
| 36 | +[project] |
| 37 | +name = "testing-uv-env" # (1) change to your project name |
| 38 | +version = "0.1.0" |
| 39 | +description = "Add your description here" |
| 40 | +readme = "README.md" |
| 41 | +requires-python = ">=3.12" |
| 42 | +dependencies = [ |
| 43 | + "instrumentserver", # (2) list every package you need here |
| 44 | + "labcore", # one string per package, comma-separated |
| 45 | +] |
| 46 | + |
| 47 | +[tool.uv.sources] |
| 48 | +# (3) for each local package listed above, add an entry here pointing to its |
| 49 | +# location on your machine. Paths are relative to this pyproject.toml file. |
| 50 | +instrumentserver = { path = "../../github/instrumentserver", editable = true } |
| 51 | +labcore = { path = "../../github/labcore", editable = true } |
| 52 | +``` |
| 53 | + |
| 54 | +**What you need to change:** |
| 55 | + |
| 56 | +1. **`name`** — set it to something that identifies your experiment or measurement folder. |
| 57 | + |
| 58 | +2. **`dependencies`** — list all packages your code needs. Regular PyPI packages (e.g. `numpy`, `matplotlib`) just go here as plain strings and uv will fetch them automatically. Local packages also go here — they need a matching entry in `[tool.uv.sources]`. |
| 59 | + |
| 60 | +3. **`[tool.uv.sources]`** — for every local package in `dependencies`, add a line with the relative path from this `pyproject.toml` to that package's root directory (the folder that contains its own `pyproject.toml` or `setup.py`). Keep `editable = true` so that code changes in those repos take effect immediately. |
| 61 | + |
| 62 | +For example, if your experiment folder is at `~/projects/my-experiment/` and `labcore` is cloned at `~/github/labcore/`, the relative path would be `../../github/labcore`. |
| 63 | + |
| 64 | +### Creating and syncing the environment |
| 65 | + |
| 66 | +From the directory containing your `pyproject.toml`, run: |
| 67 | + |
| 68 | +```bash |
| 69 | +uv sync |
| 70 | +``` |
| 71 | + |
| 72 | +This will: |
| 73 | +1. Create a `.venv` virtual environment in the current directory (if it doesn't exist). |
| 74 | +2. Install all dependencies listed in `pyproject.toml`. |
| 75 | +3. Install the local editable packages from the paths defined in `[tool.uv.sources]`. |
| 76 | +4. Generate or update `uv.lock`. |
| 77 | + |
| 78 | +### Activating the environment |
| 79 | + |
| 80 | +```bash |
| 81 | +source .venv/bin/activate |
| 82 | +``` |
| 83 | + |
| 84 | +Or run commands directly without activating: |
| 85 | + |
| 86 | +```bash |
| 87 | +uv run python my_script.py |
| 88 | +uv run jupyter lab |
| 89 | +``` |
| 90 | + |
| 91 | +### Adding new dependencies |
| 92 | + |
| 93 | +```bash |
| 94 | +uv add some-package |
| 95 | +``` |
| 96 | + |
| 97 | +This updates `pyproject.toml` and `uv.lock` automatically. |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +## Option 2: Using conda / mamba |
| 102 | + |
| 103 | +[conda](https://docs.conda.io/) is a cross-platform package and environment manager. [mamba](https://mamba.readthedocs.io/) is a faster drop-in replacement for conda that uses the same commands and environment files. |
| 104 | + |
| 105 | +### Installing conda or mamba |
| 106 | + |
| 107 | +- **Miniforge** (recommended — ships with mamba by default): |
| 108 | + Download from [github.com/conda-forge/miniforge](https://github.com/conda-forge/miniforge/releases) and follow the installer instructions. |
| 109 | + |
| 110 | +- **Miniconda** (conda only): |
| 111 | + Download from [docs.conda.io/en/latest/miniconda.html](https://docs.conda.io/en/latest/miniconda.html). |
| 112 | + |
| 113 | +### Creating an environment from the provided file |
| 114 | + |
| 115 | +A base `environment.yml` is provided at the root of this repository with a standard set of dependencies: |
| 116 | + |
| 117 | +```yaml |
| 118 | +name: labcore |
| 119 | +channels: |
| 120 | + - conda-forge |
| 121 | + - defaults |
| 122 | +dependencies: |
| 123 | + - python=3.10 |
| 124 | + - jupyterlab |
| 125 | + - jupyter_bokeh |
| 126 | + - qcodes=0.44.1 |
| 127 | + - bokeh |
| 128 | + - pandas |
| 129 | + - xarray |
| 130 | + - matplotlib |
| 131 | + - numpy=1.26.4 |
| 132 | + - scipy |
| 133 | + - scikit-learn |
| 134 | + - seaborn |
| 135 | + - lmfit |
| 136 | + - h5py=3.10.0 |
| 137 | + - xhistogram |
| 138 | + - holoviews |
| 139 | + - panel |
| 140 | + - param |
| 141 | + - hvplot |
| 142 | + - versioningit |
| 143 | + - qtpy |
| 144 | + - pip |
| 145 | + - gitpython |
| 146 | + - watchdog |
| 147 | + - pywavelets |
| 148 | +``` |
| 149 | + |
| 150 | +To create the environment from it, run from the root of the repository: |
| 151 | + |
| 152 | +```bash |
| 153 | +conda env create -f environment.yml |
| 154 | +conda activate labcore |
| 155 | +``` |
| 156 | + |
| 157 | +With mamba (faster): |
| 158 | + |
| 159 | +```bash |
| 160 | +mamba env create -f environment.yml |
| 161 | +mamba activate labcore |
| 162 | +``` |
| 163 | + |
| 164 | +### Adding or changing dependencies |
| 165 | + |
| 166 | +If you need extra packages not in `environment.yml`, install them into the active environment: |
| 167 | + |
| 168 | +```bash |
| 169 | +conda install -c conda-forge some-package |
| 170 | +``` |
| 171 | + |
| 172 | +For packages only available on PyPI: |
| 173 | + |
| 174 | +```bash |
| 175 | +pip install some-package |
| 176 | +``` |
| 177 | + |
| 178 | +### Installing local packages in editable mode |
| 179 | + |
| 180 | +For local repositories like `instrumentserver` or `labcore`, install them in editable mode using pip. Replace the paths below with the actual location of each repo on your machine: |
| 181 | + |
| 182 | +```bash |
| 183 | +pip install -e /path/to/instrumentserver |
| 184 | +pip install -e /path/to/labcore |
| 185 | +``` |
| 186 | + |
| 187 | +For example, if your repos live in `~/github/`: |
| 188 | + |
| 189 | +```bash |
| 190 | +pip install -e ~/github/instrumentserver |
| 191 | +pip install -e ~/github/labcore |
| 192 | +``` |
| 193 | + |
| 194 | +The `-e` flag means editable — any changes you make to the source code of those packages are immediately active without needing to reinstall. |
| 195 | + |
| 196 | +### Exporting and sharing your environment |
| 197 | + |
| 198 | +To export your environment so others can reproduce it: |
| 199 | + |
| 200 | +```bash |
| 201 | +conda env export > environment.yml |
| 202 | +``` |
| 203 | + |
| 204 | +To recreate it from the file: |
| 205 | + |
| 206 | +```bash |
| 207 | +conda env create -f environment.yml |
| 208 | +``` |
| 209 | + |
| 210 | +For a more portable export (cross-platform, without build strings): |
| 211 | + |
| 212 | +```bash |
| 213 | +conda env export --no-builds > environment.yml |
| 214 | +``` |
0 commit comments