VS Code Project Setup Tool
This utility automates the setup of a Python development environment in Visual Studio Code. It installs recommended extensions via the command line and generates standard configuration files (settings.json, launch.json) for linting, formatting, and debugging.
Modules Used:
- subprocess: To execute VS Code CLI commands.
- json: To generate configuration files.
- pathlib: To handle file paths and directory creation.
Prerequisites
- Visual Studio Code must be installed.
- The
codecommand must be added to your system's PATH (usually done during installation).
The Code
Save this as vscode_setup.py.
import subprocess
import json
import argparse
from pathlib import Path
import shutil
import sys
# Recommended extensions for Python development
EXTENSIONS = [
"ms-python.python",
"ms-python.vscode-pylance",
"njpwerner.autodocstring",
"njqdev.vscode-python-typehint",
"donjayamanne.githistory"
]
# Standard Python settings
SETTINGS_JSON = {
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
"python.formatting.provider": "black",
"python.linting.enabled": True,
"python.linting.pylintEnabled": True,
"editor.formatOnSave": True,
"python.analysis.typeCheckingMode": "basic"
}
# Standard Debug configuration
LAUNCH_JSON = {
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
def check_code_cli():
"""Checks if 'code' command is available."""
if not shutil.which("code"):
print("Error: VS Code CLI ('code') not found in PATH.")
return False
return True
def install_extensions():
print("Installing VS Code extensions...")
for ext in EXTENSIONS:
try:
print(f" Installing {ext}...")
# shell=True might be needed on Windows depending on how code is added to path
subprocess.run(["code", "--install-extension", ext, "--force"], check=True, shell=True)
except subprocess.CalledProcessError:
print(f" Failed to install {ext}")
def create_configs(project_dir):
vscode_dir = Path(project_dir) / ".vscode"
vscode_dir.mkdir(parents=True, exist_ok=True)
# Write settings.json
settings_path = vscode_dir / "settings.json"
if not settings_path.exists():
print(f"Creating {settings_path}...")
with open(settings_path, "w") as f:
json.dump(SETTINGS_JSON, f, indent=4)
else:
print(f"Skipping {settings_path} (already exists).")
# Write launch.json
launch_path = vscode_dir / "launch.json"
if not launch_path.exists():
print(f"Creating {launch_path}...")
with open(launch_path, "w") as f:
json.dump(LAUNCH_JSON, f, indent=4)
else:
print(f"Skipping {launch_path} (already exists).")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="VS Code Python Project Setup")
parser.add_argument("directory", nargs="?", default=".", help="Project directory (default: current)")
parser.add_argument("--no-ext", action="store_true", help="Skip extension installation")
args = parser.parse_args()
if not args.no_ext:
if check_code_cli():
install_extensions()
create_configs(args.directory)
print("\nSetup complete! You can now open the project with:")
print(f"code {args.directory}")
Usage
# Setup current directory
python vscode_setup.py
# Setup a specific project folder
python vscode_setup.py ./my_new_project