Python Development with VS Code
VSCode Setup for Python Devs
Section titled “VSCode Setup for Python Devs”Source:
Prerequisities
Section titled “Prerequisities”- Virtual Env
- Regular venv approach
- Poetry
- UV
- Formatter
- Ruff
- Options: Can be installed on OS, VS Code Extension or depency in project to ensure consistency with others
- Configure with ruff.toml
- Ruff
- Secondary Langauages
- TOML
- e.g. support pyproject.toml, ruff.toml
- TOML
Extension Recommendations and related Language Supports
Section titled “Extension Recommendations and related Language Supports”- Ruff
- Microsoft’s Python Extensions
- Python
- Pylance that has Intellisense
- Debugger
- Python
- Even Better TOML
- TOML support
- .vscode folder to share with team to easily share settings and
recommended extensions for project:
- extensions.json
- Other developers can install in VS Code
- extensions.json
Starting project and Structure
Section titled “Starting project and Structure”# Create project folder, Use version controlmkdir my_projectcd my_projecttouch .gitignore# Use virtual environment/python project, example will use uvuv init --no-workspace# README, start py filemkdir .vscodetouch settings.jsontouch extensions.json# Source code directorymkdir srcmv hello.py src/main.pycd src# Package for importingtouch __init__.pycd ..# Unit testsmkdir teststouch tests_main.py
cd ..# Dependencies## uv will manage Dependencies in pyproject.tomluv add --dev pytest
- Note for VS Codes different scopes in order of importance
- User/profile settings
- example:
- window and sidebar locations, theme, font, UI
- formatters, format on save, operations
- example:
- Workspace settings (override user settings)
- Folder settings (override workspace settings)
- Example: project settings
- Language: tests, environment files, diagnostics, binary like python venv directory
- Example: project settings
- User/profile settings
Set up pyproject.toml so tests know about src folder.
- See uv has added Dependencies
- pythonpath specifies list of modules for source
- Allows tests in tests folder to import source in src folder as if it is in the src folder
[project]name = "vscode-python"version = "0.1.0"description = "Add your description here"readme = "README.md"requires-python = ">=3.12"dependencies = [ "pytest>=8.3.3",]
[tool.pytest.ini_options]pythonpath = "src"
- Project VS Code settings.json to set test and pylance LSP settings so it knows where to find src
{ // Python settings "python.analysis.autoSearchPaths": true, "python.analysis.diagnosticSeverityOverrides": { "reportMissingImports": "none" }, // Set so Pylance knows source for tests files // Add more folders for additional src directories "python.analysis.extraPaths": [ "${workspaceFolder}/src" ], "python.envFile": "${workspaceFolder}/.env", "python.terminal.activateEnvironment": true, "python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python", // Test settings "python.testing.pytestEnabled": true, "python.testing.unittestEnabled": false, "python.testing.cwd": "${workspaceFolder}/tests", "python.testing.pytestPath": "${workspaceFolder}/.venv/bin/pytest", "python.testing.autoTestDiscoverOnSaveEnabled": true,}
More Extensions Recommendations
Section titled “More Extensions Recommendations”- GitHub Copilot - artificial intelligence help
- Better Comments - colouring coding
- GitHub Actions - CICD
- Markdown mermaid - diagrams in MD files
- Postman - for API testing
- SQLite - working with SQLite
- Dev Containers - open directories in containers