Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way in VSCode to automatically switch Python interpreter depending on the directory?

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.

like image 643
zalun Avatar asked Dec 09 '25 07:12

zalun


1 Answers

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.

like image 157
JialeDu Avatar answered Dec 10 '25 21:12

JialeDu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!