jub
jub

Reputation: 417

Why doesn't my project's virtual env auto-launch in VS Code?

VS Code v1.58.1 Python v3.92

In a python project, I set up a virtual environment. In this project folder, I have a sub-folder .vscode that contains a file settings.json, which has the following contents (pointer to the project's virtual environment folder):

{
    "python.defaultInterpreterPath": "D:\\Documents\\coding\\pyproj1\\proj_env\\Scripts\\python.exe",
    "python.terminal.activateEnvironment": true
}

When I open this project folder in VS Code, the powershell terminal does not automatically launch the virtual environment, and in the lower left corner of VS Code, the virtual environment python interpreter is not listed. When I run a new terminal in VS Code, the virtual env is still not activated.

I had the same problem when I was using python.pythonPath which I understand is now deprecated in favor of python.defaultInterpreterPath.

If I leave VS Code alone for a few minutes after opening the project folder, it will sometimes select the virtual env interpreter. But since I'm telling VS Code where it is, why isn't it selecting this at folder open?

Where am I going wrong?

Upvotes: 1

Views: 891

Answers (2)

Steven-MSFT
Steven-MSFT

Reputation: 8411

The python.defaultInterpreterPath setting only works at the first time.

After you manually select the python interpreter, the VSCode will remember it. When you reopen the VSCode, it will select the interpreter the last time you have selected. And the python.defaultInterpreterPath setting will have no influence anymore.

But you take this command to reset it: Python:Clear Workspace Interpreter Setting.

Update:

You can set the python.defaultInterpreterPath like this:

"python.defaultInterpreterPath": ".venv\\Scripts\\python.exe"

It looks like has some problem with your Python extension or the cache.

Could you try to:

  1. Reinstall the python extension. Remember to delete the extension folder under: C:\Users\${UserName}\.vscode\extensions

  2. Delete all the files under these locations:

    C:\Users\${UserName}\AppData\Roaming\Code\User\globalStorage C:\Users\${UserName}\AppData\Roaming\Code\User\workspaceStorage

Or you can empty the folder of:

C:\Users\${UserName}\AppData\Roaming\Code

But remember to storage the settings.json under

C:\Users\${UserName}\AppData\Roaming\Code\User

Upvotes: 1

jub
jub

Reputation: 417

As described in the earlier comment: "So it seems my issue is that VS Code does not remember the interpreter I used last time."

What resolved the issue is deleting the virtual env and recreating it. Now when I open the project folder, VS Code auto activates the virtual env.

Steps involved for anyone wishing to do the same:

  • activate virtual env
  • create requirements file: pip freeze > requirements.txt
  • deactivate to exit venv
  • rm proj_env or whatever the name of the venv folder is
  • create virtual env, e.g., python -m venv proj_env
  • activate the virtual env (VS Code may detect the venv during creation, and ask if you wish to activate venv, go ahead and say Yes)
  • pip install -r requirements.txt to install the requirements into the virtual env

Upvotes: 0

Related Questions