Skip to content

Commit f4dbf1d

Browse files
authored
chore(delphi): standardize venv as .venv and add Pyright config (#2560)
Editor language servers (Pyright, Pylance, VS Code, Cursor, Claude Code LSP) auto-discover `.venv` but not the previous `delphi-env`, so opening the project produced a flood of "missing import" errors for both third-party and first-party modules. Switch to the conventional `.venv` name and add `[tool.pyright]` to `delphi/pyproject.toml` so source roots, the venv location, and the Python version are explicit. The Pyright config lives inside `delphi/`, but editors look at the workspace root. To bridge that, `make venv` now also creates `polis/.venv → delphi/.venv` when the root symlink is absent, so opening Claude Code / VS Code / Cursor at the repo root finds the interpreter too. The invariant is documented in `delphi/CLAUDE.md`. Migration if you have an existing `delphi-env/` you want to keep: ln -sfn delphi-env delphi/.venv `TESTING_LOG.md` left as-is (historical log of past actions, not setup docs).
1 parent 5d4d54b commit f4dbf1d

13 files changed

Lines changed: 140 additions & 38 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ You can specify these `-f docker-compose.yml -f docker-compose.dev.yml` argument
269269
You can create your own `docker-compose.x.yml` file as an overlay and add or modify any values you need to differ
270270
from the defaults found in the `docker-compose.yml` file and pass it as the second argument to the `docker compose -f` command above.
271271

272+
To work on the **delphi** Python ML service outside Docker (run tests locally, get a working language server in your editor, etc.), see [`delphi/README.md`](/delphi/README.md#local-python-development).
273+
272274
### Testing
273275

274276
We use Cypress for automated, end-to-end browser testing for PRs on GitHub (see badge above).

delphi/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ venv/
117117
ENV/
118118
env.bak/
119119
venv.bak/
120+
# Legacy venv names (no longer canonical, but may linger in existing checkouts)
120121
delphi-env/
121122
delphi-dev-env/
122123

delphi/CLAUDE.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ this avoids the confusion of having anything called a "cid", the joke was "conve
1818

1919
this was built in two parts, the pca/kmenas/repness and the umap/narrative, and these are combined in the run_delphi.sh script.
2020

21+
## Local Python Environment
22+
23+
Canonical venv: `delphi/.venv` (Python 3.12). Setup is documented for humans in
24+
[`README.md`](README.md#local-python-development) and
25+
[`docs/QUICK_START.md`](docs/QUICK_START.md#environment-setup) — see those for
26+
the `make venv` / `uv sync` workflows.
27+
28+
Invariant to be aware of when navigating this repo: **both `delphi/.venv` and
29+
`polis/.venv` should point at the same environment** (one real, the other a
30+
symlink). The Pyright config (`[tool.pyright]` in `delphi/pyproject.toml`)
31+
resolves `venv = ".venv"` to `delphi/.venv`; editors opening at the repo root
32+
look for `polis/.venv`. If you see unresolved imports while working in this
33+
codebase, check that both paths exist and resolve to the same env.
34+
2135
## Database Interactions
2236

2337
### Querying Local PostgreSQL Database

delphi/Makefile

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,31 @@ setup-dev: install-dev ## Set up development environment
2020
@echo "2. Set up your database connection"
2121
@echo "3. Create DynamoDB tables: make setup-dynamodb"
2222

23-
venv: ## Create canonical virtual environment (delphi-env)
24-
@echo "Creating canonical virtual environment: delphi-env"
25-
@if [ ! -d "delphi-env" ]; then \
26-
python3 -m venv delphi-env; \
23+
venv: ## Create canonical virtual environment (.venv)
24+
@echo "Creating canonical virtual environment: .venv"
25+
@# Clean up a broken symlink first so `python3 -m venv` doesn't fail on
26+
@# "File exists" when .venv points at a now-missing target.
27+
@if [ -L ".venv" ] && [ ! -e ".venv" ]; then \
28+
echo "Removing broken .venv symlink"; \
29+
rm .venv; \
30+
fi
31+
@if [ ! -d ".venv" ]; then \
32+
python3 -m venv .venv; \
2733
echo "✓ Virtual environment created"; \
2834
else \
2935
echo "✓ Virtual environment already exists"; \
3036
fi
31-
@echo "To activate: source delphi-env/bin/activate"
37+
@# Mirror the venv at the repo root so editors opening the workspace at
38+
@# the parent directory (polis/) auto-discover the interpreter too.
39+
@if [ -L "../.venv" ] && [ ! -e "../.venv" ]; then \
40+
echo "Removing broken ../.venv symlink"; \
41+
rm ../.venv; \
42+
fi
43+
@if [ ! -e "../.venv" ]; then \
44+
ln -sfn delphi/.venv ../.venv; \
45+
echo "✓ Linked ../.venv → delphi/.venv (for editor auto-discovery at repo root)"; \
46+
fi
47+
@echo "To activate: source .venv/bin/activate"
3248
@echo "Then run: make install-dev"
3349

3450
venv-check: ## Check virtual environment status
@@ -43,7 +59,7 @@ venv-check: ## Check virtual environment status
4359
fi; \
4460
else \
4561
echo "❌ No virtual environment active"; \
46-
echo "Run: source delphi-env/bin/activate"; \
62+
echo "Run: source .venv/bin/activate"; \
4763
fi
4864

4965
setup-dynamodb: ## Create local DynamoDB tables

delphi/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
11
# Pol.is Math (Python Implementation)
22

3+
## Local Python development
4+
5+
Run locally (outside Docker) with either `make` or `uv`:
6+
7+
```bash
8+
# Option A: make + pip
9+
cd delphi
10+
make venv # Creates delphi/.venv + a polis/.venv symlink for editor discovery
11+
source .venv/bin/activate
12+
make install-dev # Installs delphi with dev + notebook extras
13+
14+
# Option B: uv (faster, locked deps)
15+
cd delphi
16+
uv sync # Creates delphi/.venv with all dependencies
17+
ln -sfn delphi/.venv ../.venv # One-time, for editor discovery at the repo root
18+
```
19+
20+
**Why two `.venv` paths?** Pyright (configured in `delphi/pyproject.toml`)
21+
looks for the venv at `delphi/.venv`, but editors and IDEs (VS Code, Cursor,
22+
Claude Code, JetBrains) opening the workspace at the repo root look for
23+
`polis/.venv`. Both must exist — either as the real venv directory or as a
24+
symlink to it. `make venv` creates both; the `uv` path needs the one-time
25+
symlink. If your editor's language server reports "missing imports" for
26+
`numpy`, `polismath`, etc., this is the usual cause.
27+
28+
If you have a leftover `delphi-env/` from before the rename, adopt it without
29+
reinstalling: `ln -sfn delphi-env delphi/.venv`.
30+
31+
For a full walkthrough (tests, real data, system tests), see
32+
[`docs/QUICK_START.md`](docs/QUICK_START.md).
33+
334
## Quickstart example
435

536
```bash

delphi/delphi

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
66

77
# Path to the Python CLI script and virtual environment
88
CLI_SCRIPT="$SCRIPT_DIR/scripts/delphi_cli.py"
9-
VENV_DIR="$SCRIPT_DIR/delphi-env"
9+
VENV_DIR="$SCRIPT_DIR/.venv"
1010

11-
# Check if the virtual environment exists
11+
# Clean up a broken symlink (the target was deleted or moved) so the venv
12+
# creation below doesn't fail with "File exists".
13+
if [ -L "$VENV_DIR" ] && [ ! -e "$VENV_DIR" ]; then
14+
echo "Removing broken venv symlink: $VENV_DIR"
15+
rm "$VENV_DIR"
16+
fi
17+
18+
# Check if the virtual environment exists (real dir or working symlink to one)
1219
if [ ! -d "$VENV_DIR" ]; then
1320
echo "Setting up Delphi CLI environment..."
1421
python3 -m venv "$VENV_DIR"

delphi/docs/QUICK_START.md

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,44 @@ This guide provides the essential steps to get started with the Python implement
44

55
## Environment Setup
66

7-
The Python implementation requires Python 3.8+ (ideally Python 3.12) and several dependencies.
7+
The Python implementation requires **Python 3.12** (pinned in `pyproject.toml`).
88

9-
### Creating a New Virtual Environment
9+
### Creating a Virtual Environment
1010

11-
It's recommended to create a fresh virtual environment:
11+
The recommended way is `make venv`, which also sets up the editor-discovery
12+
symlink at the repo root in one step:
1213

1314
```bash
14-
# Navigate to the delphi directory
1515
cd delphi
16-
17-
# Create a new virtual environment
18-
python3 -m venv delphi-env
19-
20-
# Activate the virtual environment
21-
source delphi-env/bin/activate # On Linux/macOS
22-
# or
23-
delphi-env\Scripts\activate # On Windows
16+
make venv # Creates delphi/.venv and (if missing) polis/.venv → delphi/.venv
17+
source .venv/bin/activate
18+
make install-dev # Installs delphi with dev + notebook extras
2419
```
2520

26-
Your command prompt should now show `(delphi-env)` indicating the environment is active.
21+
Alternatively, with [uv](https://github.com/astral-sh/uv) (faster, locked
22+
dependencies via `requirements.lock`):
2723

28-
### Installing Dependencies
24+
```bash
25+
cd delphi
26+
uv sync # Creates delphi/.venv with all dependencies
27+
ln -sfn delphi/.venv ../.venv # One-time, for editor discovery at the repo root
28+
```
2929

30-
With your virtual environment activated, install the package and its dependencies:
30+
Plain `python3 -m venv .venv` + `pip install -e ".[dev,notebook]"` also works
31+
if you prefer not to use `make` or `uv`; just remember to create the
32+
`../.venv → delphi/.venv` symlink manually so editors find the interpreter.
3133

32-
```bash
33-
# Install the polismath package in development mode
34-
pip install -e .
34+
On Windows, replace `source .venv/bin/activate` with `.venv\Scripts\activate`.
3535

36-
# Install additional packages for visualization and notebooks
37-
pip install matplotlib seaborn jupyter
38-
```
36+
### Why two `.venv` paths?
3937

40-
This will install the package in development mode with all required dependencies.
38+
Pyright's configuration (`[tool.pyright]` in `delphi/pyproject.toml`) makes
39+
`delphi/` the project root, so it looks for the venv at `delphi/.venv`.
40+
Editors and IDEs (VS Code, Cursor, Claude Code, JetBrains) opening the
41+
workspace at the repo root look for `polis/.venv`. **Both must exist**
42+
either as the real venv directory or as a symlink to it. If your language
43+
server reports unresolved imports for `numpy`, `polismath`, or similar, this
44+
is almost always the cause.
4145

4246
## Running Tests
4347

delphi/docs/RUNNING_THE_SYSTEM.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ This document provides a comprehensive guide on how to set up, run, and test the
2727
cd delphi
2828

2929
# Create a virtual environment
30-
python -m venv delphi-env
30+
python -m venv .venv
3131

3232
# Activate the virtual environment
3333
# On Linux/macOS
34-
source delphi-env/bin/activate
34+
source .venv/bin/activate
3535
# On Windows
36-
delphi-env\Scripts\activate
36+
.venv\Scripts\activate
3737
```
3838

3939
## Package Installation

delphi/notebooks/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ To run these notebooks:
2828

2929
```bash
3030
cd delphi
31-
source delphi-env/bin/activate
31+
source .venv/bin/activate
3232
jupyter lab
3333
```
3434

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33
# Activate the virtual environment
4-
source ../delphi-env/bin/activate
4+
source ../.venv/bin/activate
55

66
# Launch Jupyter Lab
77
jupyter lab

0 commit comments

Comments
 (0)