Cory McCarty
Cory McCarty

Reputation: 1405

Different Python Version/Environment Per Directory in VSCode

I have a project that includes multiple major Python components (apps that run independently) with differing dependencies and even Python versions (not ideal, but we're working on it). Each component has its own venv subdirectory which it should use for its full Python environment. What I'd like to be able to do is launch Visual Studio Code from the root of the repository (which doesn't have a venv) and have it recognize that for the purpose of linting (and any other code checking) it should use component1\venv as the Python environment/interpreter for any files that live under the component1 directory, but use component2\venv for any files that live under the component2 directory.

Currently, I can run code component1 or code component2 if I only want to work on a single component at a time (both have their own .vscode directories with python environment settings), but if I want to be able to work on multiple components (and other files that live in the root directory of the repo), Visual Studio Code uses a different Python environment (I think the system default) for everything, regardless of directory.

I've looked at related questions here, but if they suggest a solution to this, I'm missing it. If I could set different "python.pythonPath" settings per subdirectory in the root .vscode/settings.json file, that would probably do it, but I don't see a way to do that. Alternatively, if the settings were overridden by more local settings in subdirectories, it would already be working (each subdirectory already has a .vscode/settings.json that sets its correct "python.pythonPath"). Maybe this just isn't possible right now?

(Note: at the moment I'm not even concerned about actually running the components in the debugger...just getting basic coding tools and the "Problems" tab to work correctly.)

Upvotes: 6

Views: 3145

Answers (2)

Kyle Widmann
Kyle Widmann

Reputation: 70

This was something we had to figure out recently when we switched to VS Code. We couldn't find any easy answers elsewhere. For anyone else coming across this still, the easiest solution for us was the following:

  1. Open the repository in VS Code
  2. In the .vscode/settings.json file exclude directories using separate virtual envs:
"files.exclude": {
    "src/package1": true,
    "src/package2": true,
  }
  1. Add each package as a folder to the workpsace. You can use alias's via the name property if that helps make it more clear for your team.
  2. In the .vscode/settings.json file for each package added to the workspace you can set the default interpreter to the local virtual env.
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python"

After doing this we are able to have separate virtual environments, debug, and have the language server for each package resolve imports correctly.

Upvotes: 5

Brett Cannon
Brett Cannon

Reputation: 16110

I believe you're after a multi-root workspace with each individual workspace configured as appropriate.

Upvotes: 3

Related Questions