-
Notifications
You must be signed in to change notification settings - Fork 37
Python Projects Explained
This guide explains how to set up and manage Python projects in VS Code using the Python Environments extension. By the end, you'll understand what a "project" is, how to create and configure them, and how to assign the right environments and package managers to each one.
A Python Project is any file or folder that contains runnable Python code and needs its own environment. Think of it as a way to tell VS Code: "This folder (or file) is a distinct Python codebase that should use a specific Python interpreter and package manager."
By default, every workspace folder you open in VS Code is automatically treated as a project. However, you can also:
- Add subfolders as separate projects (useful for mono-repos)
- Add individual Python files as projects (great for standalone scripts)
- Create brand new projects from templates
Projects solve a common challenge: different parts of your workspace need different Python environments.
| Scenario | Without Projects | With Projects |
|---|---|---|
| Mono-repo with multiple services | All services share one environment | Each service gets its own environment |
| Testing different Python versions | Manual interpreter switching | Assign Python 3.10 to one folder, 3.12 to another |
| Shared workspace with scripts and packages | Confusing environment management | Clear separation of concerns |
The Python Environments extension adds a dedicated view to VS Code's Activity Bar. This panel has two main sections:
- Python Projects: Shows all projects in your workspace and their selected environments
- Environment Managers: Shows available environment managers (venv, conda, etc.) with their environments
There are several ways to add Python projects:
Use this when you have existing Python code that should be treated as a separate project.
- Open the Python Environments panel in the Activity Bar.
- In the Python Projects section, click the + button.
- Select Add Existing.
- Browse to and select the folder(s) or file(s) you want to add.
- Select Open to add them as projects.
Alternatively, right-click any folder or Python file in the Explorer and select Add as Python Project.
Use this to quickly discover all Python projects in your workspace based on common project markers.
- Open the Python Environments panel.
- Click the + button in the Python Projects section.
- Select Auto Find.
- The extension searches for folders containing
pyproject.tomlorsetup.pyfiles. - Select which discovered projects to add from the list.
Tip: Auto-find is especially useful when you clone a mono-repo and want to quickly identify all its Python projects.
Use this to scaffold a brand new Python project with the correct structure and files.
- Open the Command Palette (
Cmd+Shift+Pon macOS,Ctrl+Shift+Pon Windows/Linux). - Run Python Envs: Create New Project from Template.
- Choose a template type:
-
Package: A structured Python package with
pyproject.toml, tests folder, and package directory - Script: A simple standalone Python file using PEP 723 inline metadata
-
Package: A structured Python package with
- Enter a name for your project.
- Choose whether to create a virtual environment.
The extension creates the project structure, adds it to your workspace, and optionally creates a virtual environment.
When you create a package named my_package, the extension generates:
my_package_project/
├── pyproject.toml # Project metadata and dependencies
├── dev-requirements.txt # Development dependencies
├── my_package/ # Your package source code
│ └── __init__.py
└── tests/ # Test directory
└── __init__.py
When you create a script, the extension generates a single .py file with PEP 723 inline script metadata, which allows you to specify dependencies directly in the file.
Each project can have its own Python environment. This is the core benefit of project management.
- In the Python Projects section, find your project.
- Click on the environment path shown beneath the project name (or "No environment" if none is set).
- Select an environment from the list of available environments.
You can also:
- Click the environment icon next to a project
- Right-click the project and select Set Project Environment
- In the Environment Managers section, expand a manager (e.g., venv, conda).
- Find the environment you want to use.
- Right-click it and select Set As Project Environment.
- Choose which project should use this environment.
Beyond selecting which Python interpreter a project uses, you can also specify which environment manager and package manager the project should use. This affects how environments are created and how packages are installed.
Environment managers control how Python environments are created and discovered:
| Manager | Description |
|---|---|
venv |
Built-in Python virtual environments (default) |
conda |
Conda environments from Anaconda or Miniconda |
pyenv |
Multiple Python versions via pyenv |
poetry |
Poetry-managed environments |
pipenv |
Pipenv-managed environments |
system |
System-installed Python interpreters |
The extension automatically searches for virtual environments in specific locations. By default, venv environments are discovered in ./**/.venv (any .venv folder within your workspace). You can customize these search paths using the python-envs.venv.searchPaths setting.
When you add a project, its folder is automatically added to the environment search path. This means environments inside project folders (e.g., my-project/.venv) will be discovered automatically. You may need to reload VS Code after adding new projects for all environments to appear.
Package managers control how packages are installed in environments:
| Manager | Description |
|---|---|
pip |
Standard Python package installer (default) |
conda |
Conda package manager for conda environments |
- Right-click a project in the Python Projects section.
- Select Set Environment Manager to change how environments are created.
- Select Set Package Manager to change how packages are installed.
Note: The default package manager is typically determined by the environment manager. For example, venv environments use pip by default, while conda environments use the conda package manager.
The extension stores project configurations in your VS Code settings. Understanding this helps you manage settings across different scopes.
Project settings are stored in the python-envs.pythonProjects setting. Depending on your workspace setup:
| Workspace Type | Settings Location |
|---|---|
| Single folder |
.vscode/settings.json in your workspace |
| Multi-root workspace |
.code-workspace file |
The pythonProjects setting is an array of project configurations:
{
"python-envs.pythonProjects": [
{
"path": "backend",
"envManager": "ms-python.python:venv",
"packageManager": "ms-python.python:pip"
},
{
"path": "ml-service",
"envManager": "ms-python.python:conda",
"packageManager": "ms-python.python:conda"
}
]
}Each project entry contains:
| Property | Description |
|---|---|
path |
Relative path from workspace root to the project |
envManager |
ID of the environment manager (e.g., ms-python.python:venv) |
packageManager |
ID of the package manager (e.g., ms-python.python:pip) |
workspace |
(Multi-root only) Name of the workspace folder containing the project |
You can set default managers that apply to all projects without explicit overrides:
{
"python-envs.defaultEnvManager": "ms-python.python:venv",
"python-envs.defaultPackageManager": "ms-python.python:pip"
}Multi-root workspaces contain multiple top-level folders. The extension handles these seamlessly:
- Each workspace folder is automatically treated as a project.
- You can add sub-projects within any workspace folder.
- Project settings include a
workspaceproperty to identify which folder they belong to. - When creating a new project from a template, you're prompted to select which workspace folder to use.
my-workspace.code-workspace
├── frontend/ → Project with Python 3.12
│ └── scripts/ → Sub-project with same environment
├── backend/ → Project with Python 3.10, venv
│ ├── api/ → Sub-project with its own venv
│ └── workers/ → Sub-project with its own venv
└── ml-pipeline/ → Project with conda environment
To remove a project (this does not delete any files):
- Right-click the project in the Python Projects section.
- Select Remove Python Project.
The project is removed from the extension's tracking. Its files remain untouched, and you can always add it back later.
Access these via the Command Palette (Cmd+Shift+P / Ctrl+Shift+P):
| Command | Description |
|---|---|
| Python Envs: Create New Project from Template | Create a new package or script from a template |
| Python Envs: Add Python Project | Add existing files/folders as projects |
| Python Envs: Set Project Environment | Change the Python interpreter for a project |
| Python Envs: Set Environment Manager | Change how environments are created |
| Python Envs: Set Package Manager | Change how packages are installed |
| Python Envs: Create Environment | Create a new environment for a project |
| Python Envs: Manage Packages | Install or uninstall packages |
| Setting | Default | Description |
|---|---|---|
python-envs.defaultEnvManager |
"ms-python.python:venv" |
Default environment manager for new projects |
python-envs.defaultPackageManager |
"ms-python.python:pip" |
Default package manager for new projects |
python-envs.pythonProjects |
[] |
List of project configurations |
You have a repository with multiple Python services, each needing isolated dependencies.
- Open the root folder in VS Code.
- Click + in Python Projects and select Auto Find to discover all services with
pyproject.toml. - Select the services you want to track.
- For each service, create a virtual environment by right-clicking and selecting Create Environment.
- Each service now runs with its own isolated environment.
Your data science project needs packages that install more reliably with conda.
- Right-click the project in Python Projects.
- Select Set Environment Manager → conda.
- Select Set Package Manager → conda.
- Right-click the project again and select Create Environment to create a new conda environment.
You have a utility script that needs specific packages without affecting your main project.
- Right-click the
.pyfile in Explorer. - Select Add as Python Project.
- The script is now a separate project that can have its own environment.
You have a workspace containing several Python packages, each with its own dependencies and requiring isolated environments.
my-workspace/
├── core-lib/ # Shared utilities package
│ └── pyproject.toml
├── auth-package/ # Authentication package
│ └── pyproject.toml
└── data-models/ # Data models package
└── pyproject.toml
- Open the workspace root in VS Code.
- Click + in Python Projects and select Auto Find to discover all packages with
pyproject.toml. - Select each package folder to add as a project.
- For each package, right-click and select Create Environment to create a dedicated virtual environment.
- Each package now has its own isolated environment for development and testing.
Tip: When one package depends on another (e.g.,
auth-packageimports fromcore-lib), install the dependency as an editable package in the dependent environment usingpip install -e ../core-lib.
- Verify the file or folder is inside your workspace
- Check if it was already added (duplicates are prevented)
- Try using Add Existing and manually selecting it
- Restart any open terminals to use the new environment
- Check that the environment actually exists and is valid
- Verify the
pythonProjectssetting in.vscode/settings.json
- Ensure you have write access to the workspace folder
- Check if settings are being overridden at a higher scope (User vs. Workspace)
- For multi-root workspaces, verify the
.code-workspacefile is being saved