'VS Code Doesn't Recognize Python Virtual Environment

I'm using VS Code on a Mac to write Python code. I've created a virtual environment named 'venv' inside my project folder and opened VS Code in my project folder. I can see the venv folder in the Explorer pane. However, if I install a package into the virtual environment and I try to import the package into a Python module and then run the module, VS Code raises a ModuleNotFound error saying there is no module.

I followed the instructions in the VS Code document Using Python environments in VS Code by opening the Command Palette, choosing Python: Select Interpreter, and then selecting "venv/bin/folder". But when I do that, I get this error:

Failed to set 'pythonPath'. Error: Unable to write into folder settings.  Please open the 'my_project' folder settings to correct errors/warnings in it and try again.

What are these "folder settings?" I don't see anything in the document I cited above that talks about a folder setting for my virtual environment directory.

Environment:
VS Code 1.35.1
Python for VS Code 0.2.3
Python 3.7.1

UPDATE

Taking @khuynh advice, I opened settings.json and found one error which was that I tried to comment out a line with "//". I didn't realize that JSON files can't include comments.

After taking that line out, I ran "Python: Select Interpreter" again but this time a tab that says .vscode > settings.json >> code-runner.executorMap.python at the top appeared. The tab contained the following code:

    {
        "python.pythonPath": "/usr/local/bin/python3"
        "code-runner.executorMap.python": "python3 -u"
    }

There is a red squiggly line beneath "code-runner.executorMap.python" and the Problems window below says "Unknown configuration setting." I don't understand what's wrong with this setting.



Solution 1:[1]

It seems that now is called python.defaultInterpreterPath

Solution 2:[2]

Selecting a Python Interpreter does not work if you have errors in your workplace settings file. You are missing a comma after your "python.pythonPath" line.

(I see that others answered this in comments, but I wanted to post an Answer to make this easier for others to find.)

Solution 3:[3]

It sounds like you opened a Python file directly versus opening the folder containing the file. If you do the latter then you can specify the Python interpreter in your settings.

Otherwise you may have permission errors that are preventing creating the .vscodefolder to save your settings.

Solution 4:[4]

To set up Visual Studio Code on a Mac with a python virtual environment edit the JSON workspace settings in Visual Studio Code with the shortcut "command + shift + p"

Preferences: Open Workspace Settings (JSON)

In Visual Studio Code Version: 1.45.1 the workspace.json settings look like this:

{
    "folders": [
          {
              "path": "/Users/me/Documents/Projects/djangoproject"
          }
      ],
      "settings": {
          "python.pythonPath": "/Users/me/virtualenvs/djangovenv/bin/python3"
      }
}

This will automatically activate the python virtual environment when a new bash terminal is opened on a Mac.

Solution 5:[5]

Just now, I reopened a VS Code workspace on an external drive after moving from work PC to home PC and encountered this error.

I solved it by opening VS code settings cmd+shift+p and then type in "settings" and select the option "Preferences: Open Settings (JSON)"

then I edited the line: "python.pythonPath": "C:\\Python37\\python3.exe",

to the following (check the path to python executable): "python.pythonPath": "C:\\Python37\\python.exe",

to fix.

Update: What occurred to me is that I may face this issue again when I access the project from my work computer next week as I have a different python version installed there. Will post outcome

Solution 6:[6]

For me the python plugin in wsl2 was missing. Installing that and changing to python.defaultInterpreterPath (new) fixed it for me.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 aleGpereira
Solution 2 jjw
Solution 3 Brett Cannon
Solution 4 kingurr
Solution 5
Solution 6 Joe