VS Code Doesn't Recognize Python Virtual Environment
Asked Answered
S

7

7

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.

Strachan answered 18/6, 2019 at 15:57 Comment(3)
It looks like there could be an error in your settings.json. Try opening up your command palette using cmd+shift+p and then type in "settings" and select the option "Preferences: Open Settings (JSON)" and look for any red squiggly lines indicating an error. I'd double check your paths to make sure they are correct as wellLorolla
I took your advice which helped clear up one error but I still have a second error as I described in the UPDATE section above.Strachan
In your error, there should be a comma after the first key-value pair. So you should change that line to: "python.pythonPath": "/usr/local/bin/python3",.Lorolla
E
19

It seems that now is called python.defaultInterpreterPath

Enthetic answered 15/2, 2022 at 16:42 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewArias
F
3

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.)

Fontaine answered 6/7, 2019 at 14:35 Comment(0)
N
2

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.

Nucleon answered 18/6, 2019 at 23:17 Comment(0)
H
2

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.

Hirohito answered 5/6, 2020 at 16:39 Comment(0)
K
0

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

Kp answered 6/9, 2019 at 7:51 Comment(1)
please post the outcomeKoan
H
0

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

Heterophyte answered 6/10, 2021 at 8:9 Comment(0)
U
0
  1. Make sure you already installed Python extension on your VS Code. Here is the link: https://marketplace.visualstudio.com/items?itemName=ms-python.python

enter image description here

  1. python.pythonPath seems changed to python.defaultInterpreterPath

Another way is you can use SHIFT + COMMAND + P then select interpreter.

enter image description here

This is my settings file. Hope it helpful to someone.

{
    "python.defaultInterpreterPath": "venv/bin/python",
    "python.linting.flake8Enabled": true,
    "python.linting.flake8Args": [
        "--max-line-length=120"
    ],
    "python.linting.pylintEnabled": false,
    "python.linting.enabled": true,
    "python.linting.banditEnabled": false,
    "python.linting.mypyEnabled": false,
    "python.formatting.autopep8Args": ["--max-line-length", "250", "--experimental"],
    "editor.wrappingIndent": "indent",
    "editor.autoIndent": "keep",
    "workbench.colorCustomizations": {
        "tab.activeBackground": "#777a7a"
    },
    "[python]": {
        "editor.formatOnSave": true,
        "editor.insertSpaces": true,
        "editor.tabSize": 2,
    },
    "[yaml]": {
        "editor.formatOnSave": true,
        "editor.insertSpaces": true,
        "editor.tabSize": 2,
        "editor.quickSuggestions": {
            "other": true,
            "comments": false,
            "strings": true
        },
        "editor.autoIndent": "keep"
    },
    "files.trimTrailingWhitespace": true,
    "files.insertFinalNewline": true,
    "editor.rulers": [
        {
            "column": 120,
            "color": "#424142"
        },
    ],
}
Uriiah answered 23/2, 2023 at 7:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.