1

I have a project workspace with multiple microservices, how can I make VS Code switch between python interpreters to the nearest virtual environment folder?

For example:

  • Workspace/folder1/src/main.py uses Workspace/folder1/.venv/
  • Workspace/folder2/src/main.py uses Workspace/folder2/.venv/
  • Workspace/folder3/src/main.py uses Workspace/.venv/
  • Workspace/folder4/toolsfolder/tool1/main.py uses Workspace/folder4/toolsfolder/tool1/.venv/
Workspace/
├── folder1/
│   ├── .venv/
│   └── src/
│       └── main.py
├── folder2/
│   ├── .venv/
│   └── src/
│       └── main.py
├── folder3/
│   └── src/
│       └── main.py
├── folder4/
│   └── toolsfolder/
│       ├── tool1
│       │   ├── .venv/
│       │   └── main.py
│       ├── tool2
│       │   ├── .venv/
│       │   └── main.py
│       └── README.md
├── .venv/
├── Makefile
└── README.md
2
  • I'm not sure this is possible to do automatically. There are settings flags for automatically activating virtual environments, but I don't think it's triggered just by clicking into a directory in your workspace and I don't know if it sets the interpreter. You can always use the Select interpreter command to manually switch. Commented Jun 21, 2022 at 17:23
  • "python.terminal.activateEnvironment": true and "python.defaultInterpreterPath": "~/venv/bin/python" I think are the relevant settings, but I've never needed this functionality so I'm not certain. Commented Jun 21, 2022 at 17:26

2 Answers 2

1

If you want to automatically activate the correct environment and start debugging the current active editor you have to split the starting the debugger and attach the client (VSC) to the debugger.

Write a shell script that has as argument the current file path. The script will activate the correct Python environment and start a debugger on the given file path that waits for the client.

I have described this process and what to use as commands in another Python Debug answer

The task to use is:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Script to activate specific environment and debug",
      "type": "shell",
      "command": "activate-env-and-debug ${workspaceFolder} ${relativeFile}",
      "problemMatcher": []
    }
  ]
}

In the script to start the debugger use a command like:

python <path>/.vscode/extensions/ms-python.python-2021.12.1559732655/pythonFiles/lib/python/debugpy
  --listen 5678 --wait-for-client $1/$2

Because the environment is active python should execute the correct interpreter.

The version number of the ms-python.python extension will vary after updates, maybe you can extract the current (latest) by enumerating the extensions folder and search for the one with the largest version number (by parts) (they don't use 2 months digits)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your input. I set a launch configuration using the python property to select the specific Python interpreter. Do you know if it's possible to trigger multiple launch configurations to start multiple debuggers?I saw that we could use dependsOn with tasks, but I don't find a way to activate multiple launches at once.
@DanielDias You can setup Multi-target debugging, but with the python launch property you have to create a launch config for every virt environment
0

I don't understand why you want to use multiple environments in the same workspace.

I think it would be better to separate them in different workspaces in order to avoid environmental chaos.

The chosen python interpreter is specific to the current workspace, instead of an independent directory. If you like, you could manually select the interpreter (ctlr+shift+P) when using different folders.

2 Comments

It's a platform project, and it has multiple microservices APIs that were developed over time. While developing it, we run all APIs to run the platform locally. To run it is fine; we have a makefile that goes through all APIs, activates the python venv, and runs the API, but to debug the code, I need to run it with VSCode debug, and in this case, it will use the VSCode Python interpreter.
I imagine this same use case with a monorepo. You want the whole repo in one workspace, but you want separate venv for each service.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.