14

I'm using a monorepo with multiple packages in separate directories. Poetry is in charge of package management including creating the virtual environment.

I wish VS Code would use the right Python interpreter for each package individually. Is that possible? My workaround is to open a separate window with the directory containing a package, so all of its files would use the right interpreter.

1
  • use Multi Root Workspace, now you have a .vscode/settings.json for each folder and that can contain a separate Python Interpreter path
    – rioV8
    Commented Oct 18, 2022 at 12:51

1 Answer 1

8

You can open multiple folders in vscode and select the interpreter for each folder individually.

  1. Select Add Folder to Workspace in the menu. As a simple example I have added three folders

    enter image description here

    enter image description here

  2. Then click on the python interpreter version in the lower right corner (or Ctrl+Shift+P --> Python:Select Interpreter)

    enter image description here

  3. Choose a folder, here I choose folder1

    enter image description here

  4. Now select interpreter for folder1 folder

    enter image description here

The same operation continues to select interpreters for folder2 and folder3.

Of course this is just the basics, you can also create virtual environments for each folder and then choose the interpreter for the virtual environment for each folder.

For example, I created a virtual environment .venv2 under the folder2, the same step 2 and 3, and then I selected the interpreter under the virtual environment .venv2.

enter image description here

There is also a way to specify the default interpreter path in the settings.json, but after setting it also requires step 2 and 3 and then select it in the selection panel.

enter image description here

When you modify the folder settings, the .vscode folder and the settings.json file will be automatically generated under the folder. The contents of the settings.json file are the settings you just modified

enter image description here

And this step is reversible. You can manually create the .vscode folder and add the settings.json file in it. And enter the corresponding settings in the settings.json file.

2
  • Thank you. I was also wondering about the tests. The tests seem to run outside of the chosen environments... Maybe I'll create a separate issue for that
    – zalun
    Commented Oct 19, 2022 at 11:28
  • 2
    Thanks @JialeDu but adding the folders in this manner means that vscode no longer picks up the root of the folder which contains .gitignore and git itself - any suggestions?
    – dendog
    Commented Jul 25, 2023 at 18:37

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.