| title | Python Development in LXD (plus VS Code Remote SSH) |
|---|
This guide outlines how to set up a Python development environment inside an LXD container using uv, with full support for VS Code Remote SSH.
Inside the container as the ubuntu user:
mkdir ~/app
cd ~/appYou may choose a different name (e.g., sandbox, workspace) depending on your project.
uv automatically downloads and manages Python versions. To create a virtual environment using Python 3.13:
uv venv --python 3.13
source .venv/bin/activateThis ensures your environment uses Python 3.13 without modifying the system Python.
For the latest version run
uv venvwithout the --python flag
Create a .vscode directory in your project:
mkdir .vscode{
"editor.formatOnSave": true,
"files.trimTrailingWhitespace": true,
"files.insertFinalNewline": true,
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports.ruff": "explicit"
}
},
"python.analysis.typeCheckingMode": "strict"
}{
"recommendations": [
"ms-python.python",
"redhat.vscode-yaml",
"ms-python.black-formatter",
"charliermarsh.ruff",
"ms-python.mypy-type-checker"
]
}From the root of your project folder (e.g. ~/app), run:
uv init --appThis will create a scaffold including pyproject.toml, .venv, .gitignore, and more.
| Flag | Description |
|---|---|
--lib |
Create a reusable library (Python package) |
--app |
Create an application (scriptable or runnable project) |
--script |
Create a minimal script-style project |
--package |
Same as --lib, sets up for distribution |
--no-package |
Avoid packaging setup |
Once the scaffold is created, append the following to the bottom of pyproject.toml to add your dev tooling preferences:
[project.optional-dependencies]
dev = ["ruff", "black", "mypy"]
[tool.black]
line-length = 88
target-version = ["py310"]
skip-string-normalization = false
[tool.ruff]
line-length = 88
target-version = "py310"
extend-select = ["I"]
fix = true
[tool.mypy]
python_version = "3.10"
strict = true
ignore_missing_imports = trueUse the Remote SSH extension in VS Code to connect to your container.
Once connected, open the ~/app folder.
VS Code should:
- Prompt you to install the recommended extensions.
- Detect the
.venvcreated byuv init. - Use Python 3.13 (via
.venv) as the interpreter automatically.
If it doesn’t, press Ctrl+Shift+P or Cmd+Shift+P and run:
Python: Select Interpreter → choose the one from
.venv/bin/python
You're now ready to develop in a modern, versioned Python environment fully integrated with VS Code.
uv venv --python 3.11.6Note: This will not adjust .python-version which may cause issues with tools that reference it.
Edit the .python-version file to contain just the desired Python version number:
3.11.6
Then run:
uv syncThis will recreate the environment using the specified version.
In your pyproject.toml file, update the value:
requires-python = ">=3.11"This ensures compatibility metadata reflects the active Python version.