2

Background: I am a developer on a team that uses Python and uses VS Code heavily. We'd like to be able to share our project.code-workspace but one of the critical settings is python.pythonPath which specifies the Python executable.

Because we use virtualenvs (and at least 2 different operating systems), we can't specify this setting, it will be a different absolute path on various machines.

We also can't not specify, it, though; without it set, many features of the Python extension don't work. As soon as anyone sets it locally in that workspace, the file is modified in version control. We constantly risk someone changing it and then committing their change, breaking it for everyone else.

The result is that we are unable to commit a .code-workspace file because it can't be parameterized.

Cory
  • 1,530
  • 2
  • 11
  • 15
  • Does this answer your question? [Setting the Python path for local project in VS Code without using the settings.json file](https://stackoverflow.com/questions/56825741/setting-the-python-path-for-local-project-in-vs-code-without-using-the-settings) – neves Mar 16 '21 at 22:14

1 Answers1

2

After some trial-and-error, I found some answers.

First of all, make sure your virtualenv is visible to VS Code's Python extension

Two settings appear to control where the Python extension looks for interpreters and environments, python.venvPath and python.venvFolders. The first one is officially documented on the settings reference. I can't find any official documentation for the second one, but there are lots of references to it on the Internet.

I set my paths as follows. We use virtualenvwrapper.

I set my paths as follows. We use virtualenvwrapper.

I recommend setting these at either the User or Workspace level. If you have some team members using different types of virtualenvs, or installing venvs somewhere other than the default, you might want to keep this at the User level so it can be modified by them.

Second, make sure you haven't accidentally set it somewhere else

VS Code has multiple settings levels; Default < User < Workspace < Folder.

In my case (and for many other team members) we had a python.pythonPath setting at the Folder level, which overrides all the others. The Folder level has the highest priority.

You may need to open and edit settings.json at each level and delete the setting. Once you have done so, VS Code should pick up the path to Python in your virtualenv automatically, meaning you don't have to set it at all.

If you still must set the python.pythonPath manually . . .

... I recommend setting it at the Folder level and hiding ${workspaceFolder}/.vscode from version control. This allows you to:

  • Give each user the ability to set it themselves

  • Give users the ability to set it separately for each project as they switch between projects.

And again, make sure your team doesn't accidentally change this setting at the Workspace level, so you can keep your project.code-workspace file clean in version control.

Cory
  • 1,530
  • 2
  • 11
  • 15